问题描述
由于
- 现代的Linux / UNIX / OSX(W /
的真实路径
) -
庆典
(甚至在OSX)4+
- A modern Linux/UNIX/OSX (w/
realpath
) bash
4+ (even on OSX)
时
"$PWD" == "$(realpath .)"
始终是真的吗?
推荐答案
它是pretty容易测试,这并非总是如此。
It's pretty easy to test that this is not always the case.
$ mkdir /tmp/realdir
$ cd /tmp/realdir
$ echo $PWD
/tmp/realdir
$ ln -s realdir /tmp/fakedir
$ cd /tmp/fakedir
$ echo $PWD
/tmp/fakedir
$ realpath .
/tmp/realdir
所以没有, $ PWD
并不总是相同的 $(真实路径。)
。
借助表明 PWD
变量是由内置的 CD
命令设置。默认的href=\"http://www.gnu.org/software/bash/manual/bashref.html#Bourne-Shell-Builtins\" rel=\"nofollow\">行为是:
The bash manual indicates that the PWD
variable is set by the built-in cd
command. the default behaviour of cd is:
符号链接后违约或带有-l选项
这意味着,如果CD插入符号链接的变量被相对于符号链接,而不是相对于物理路径解决。您可以通过使用 -P
选项更改此行为为 CD
命令。这将导致其报告中的 PWD
变量的物理目录:
This means that if you cd into a symlink the variable gets resolved relative to the symlink, not relative to the physical path. You can change this behavior for a cd
command by using the -P
option. This will cause it to report the physical directory in the PWD
variable:
$ cd -P /tmp/fakedir
$ echo $PWD
/tmp/realdir
您可以使用 -P
选项来改变bash中的默认行为:
You can change the default behavior of bash using the -P
option:
$ set -P
$ cd /tmp/fakedir
$ echo $PWD
/tmp/realdir
$ set +P
$ cd /tmp/fakedir
$ echo $PWD
/tmp/fakedir
这是当然的,尽管事实上,你可以执行后CD
PWD 变量什么>它需要的值:
This is of course notwithstanding the fact that you can assign anything you want to the PWD
variable after performing a cd
and it takes that value:
$ cd /tmp/fakedir
$ PWD=/i/love/cake
$ echo $PWD
/i/love/cake
但是这不是真的你是问。
but that's not really what you were asking.
这篇关于请问$ PWD总是等于$(真实路径。)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!