<分区>
请参阅下面的 Unix 命令。
当我们处理软链接(soft link)时,从每个目录到根目录可以有多个路径。
那么这种情况下,pwd
和cd ..
是怎么计算的呢?这意味着目录路径不再是无状态的,对吧?
$ cd ~
$ mkdir a b
$ cd a
$ ln -s ~/b b
$ cd b
$ pwd
/home/myuser/a/b
$ cd ..
$ pwd
/home/myuser/a
<分区>
请参阅下面的 Unix 命令。
当我们处理软链接(soft link)时,从每个目录到根目录可以有多个路径。
那么这种情况下,pwd
和cd ..
是怎么计算的呢?这意味着目录路径不再是无状态的,对吧?
$ cd ~
$ mkdir a b
$ cd a
$ ln -s ~/b b
$ cd b
$ pwd
/home/myuser/a/b
$ cd ..
$ pwd
/home/myuser/a
最佳答案
看这个例子:
[myuser@test ~]$ pwd
/home/myuser
[myuser@test ~]$ mkdir a b
[myuser@test ~]$ cd a
[myuser@test a]$ ln -s ~/b b
[myuser@test a]$ cd b
[myuser@test b]$ pwd
/home/myuser/a/b
[myuser@test b]$ pwd -P
/home/myuser/b
[myuser@test b]$ echo $$
2432
[myuser@test b]$ ls -l /proc/2432/cwd
lrwxrwxrwx 1 myuser myuser 0 Oct 4 04:10 /proc/2432/cwd -> /home/myuser/b
[myuser@test b]$
[myuser@test b]$
[myuser@test b]$ pwd
/home/myuser/a/b
[myuser@test b]$ cd -P ..
[myuser@test ~]$ pwd
/home/myuser
[myuser@test ~]$
[myuser@test ~]$ env | grep "PWD"
PWD=/home/myuser
OLDPWD=/home/myuser/a/b
请参阅 bash 手册中的选项 -P 到 cd:
-P If set, the shell does not follow symbolic links when executing commands such as cd that
change the current 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.
如您所见,内核保留的当前目录是您的真实目录(/proc/2432/cwd ->/home/myuser/b)但是 bash 可以做任何想做的事情,是否遵循符号链接(symbolic link), 因为 cd 是 bash 内部命令。
关于linux - 当您涉及软链接(soft link)时, "cd .."和 "pwd"究竟意味着什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26190114/