cd/bash在符号链接上的行为

Her*_*che 26 linux filesystems bash symlink

假设我的主文件夹中有〜/ a/b文件夹,文件夹b包含一个名为'symlink'的'..'的符号链接.然后我在bash中执行以下操作:

hm@mach:~$ cd a/b/symlink
hm@mach:~/a/b/symlink$ pwd -P
/home/hm/a
hm@mach:~/a/b/symlink$ cd ..
hm@mach:~/a/b$ pwd -P
/home/hm/a/b
Run Code Online (Sandbox Code Playgroud)

pwd -P打印当前工作目录,取消引用所有符号链接.为什么工作目录/ home/hm/a/b在最后,而不是/ home/hm?

sar*_*old 27

bash正在"友好"; 当你cd /into/a/symlink/,shell记住旧位置(in $OLDPWD)时,如果你想要返回到你刚刚进入的目录,它将使用目录cd ...

如果你想使用真实 ..,那么你还必须使用cd -P:

          The -P option says to use the physical directory
          structure instead of following symbolic links (see
          also the -P option to the set builtin command);
          the -L option forces symbolic links to be followed.
Run Code Online (Sandbox Code Playgroud)
$ cd
$ cd a/b/symlink
$ cd -P ..
$ pwd -P
/home/sarnold
$ 
Run Code Online (Sandbox Code Playgroud)

  • 仅供参考,你可以使用`set -o physical`或`set -P`来使这种行为永久化. (9认同)

gee*_*aur 5

bash跟踪逻辑当前目录路径,如提示中所示,并cd ..根据此解释事物.如果你只是在cd(或pushd)中使用这样的路径,这会使事情变得更加一致,如果你希望在命令参数(或命令内部)中的路径发生同样的事情,emacs并且vim有自己的可配置规则,则会以意外事件为代价对于符号链接处理,但大多数命令依赖于内核来处理它).