防止“cd”记住遍历的符号链接

Ste*_*ett 4 shell bash directory symbolic-link

假设您有目录 ~/mytool 软链接到 /usr/local/mytool。然后cd mytool将当前目录保留为 ~/mytool,这可能会导致脚本行为不正确。有没有办法避免这种行为?

通过一些谷歌搜索,我发现您可以按如下方式实现:

cd $1 ; cd `pwd -P`
Run Code Online (Sandbox Code Playgroud)

没有切换到'cd'吗?环境变量?

Mat*_*erg 8

如果您输入set -Pbash 所有命令,例如 cd,pwd 将遵循物理路径。否则,您可以使用cd -Ppwd -P临时更改默认行为。

从 bash 的联机帮助页:

          -P      If  set,  the shell does not follow symbolic links when executing commands such as cd that change the cur-
                  rent working directory.  It uses the physical directory structure instead.  By default, bash  follows  the
                  logical chain of directories when performing commands which change the current directory.
Run Code Online (Sandbox Code Playgroud)

例如,要使其永久化,请将其放入您的~/.bashrc文件中。