unix 内置 `pwd` 命令和它的 $PWD 环境变量有什么区别?

发布于 2024-10-06 08:15:20 字数 294 浏览 1 评论 0原文

情况如下。 我有一个目录呼叫:- %/home/myname/

我在该目录中做了一个软链接:- %cd /home/我的名字/ %ln -s /home/others/ .

现在,我从 /home/myname/ cd 到 other/ 这是有趣的部分。

当我执行 unix 内置 pwd 命令时,我得到原始路径名:- %/home/others/

但是当我回显 $PWD 环境变量时,我得到链接路径名称:- %/home/myname/others/

为什么会这样?

Here's the case.
I have a directory call :-
%/home/myname/

I did a soft link in that directory:-
%cd /home/myname/
%ln -s /home/others/ .

Now, I cd into others/ from /home/myname/
Here's the interesting part.

When I did a unix built-in pwd command, i get the ORIGINAL path name:-
%/home/others/

But when i echo the $PWD environment variable, i get the link path name:-
%/home/myname/others/

Why is that so?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

寄居人 2024-10-13 08:15:20
/var# ls -l
lrwxrwxrwx  1 root root   10 Aug 22 13:21 mail -> spool/mail
drwxr-xr-x  2 root root 4096 Jul  1 20:58 opt
drwxr-xr-x 22 root root 4096 Dec  5 17:38 run
drwxr-xr-x 12 root root 4096 Aug 22 13:21 spool
drwxrwxrwt 14 root root 4096 Dec  6 02:46 tmp
/var# cd mail
/var/mail# echo $PWD
/var/mail

/var/mail# pwd
/var/mail

/var/mail# /bin/pwd
/var/spool/mail

换句话说,使用 $PWD 就足够了,因为无论如何 pwd 可能不会给你更好的结果(对于更好的任何定义)。

这是为什么呢? /bin/pwd 使用特定于操作系统的调用来确定当前工作目录 - 对于 Linux,内核仅保留已解析的目录(请参阅 /proc/self/cwd >),而 shell 的 pwds 包含 shell 认为它所在的内容。

/var# ls -l
lrwxrwxrwx  1 root root   10 Aug 22 13:21 mail -> spool/mail
drwxr-xr-x  2 root root 4096 Jul  1 20:58 opt
drwxr-xr-x 22 root root 4096 Dec  5 17:38 run
drwxr-xr-x 12 root root 4096 Aug 22 13:21 spool
drwxrwxrwt 14 root root 4096 Dec  6 02:46 tmp
/var# cd mail
/var/mail# echo $PWD
/var/mail

/var/mail# pwd
/var/mail

/var/mail# /bin/pwd
/var/spool/mail

In other words, using $PWD is sufficient, because pwd might not give you better results (for any definition of better) anyway.

Why that is? /bin/pwd uses OS-specific calls to determine the current working directory - and in case of Linux, the kernel only keeps the resolved directory (see /proc/self/cwd), whereas the shell's pwds contain what the shell thinks it is in.

月亮是我掰弯的 2024-10-13 08:15:20

/bin/pwd 外部命令和内置命令之间的区别在于,外部命令不知道哪一组 cd 操作让您到达那里,因此不会'不要假装您的当前目录位于符号链接链的某个位置;它为您提供从根目录到当前目录的直接路径,就像 realpath() 函数一样。

请参阅 bash 中的 set -ophysical

The difference between the /bin/pwd external command and the built-in is that the external command doesn't know what set of cd operations got you there and therefore doesn't pretend that your current directory is somewhere down a chain of symlinks; it gives you the direct path from root to your current directory, rather like the realpath() function would.

See set -o physical in bash.

东走西顾 2024-10-13 08:15:20

/bin/pwd 使用操作系统特定的调用来确定当前工作目录。

您可以通过 Git 2.34(2021 年第 4 季度)进行说明,该版本必须将 $(pwd) 替换为 $PWD,并解释了原因。

请参阅 提交 f6a5af0(2021 年 8 月 24 日),作者:Johannes Sixt (j6t)
(由 Junio C Hamano -- gitster -- 合并于 提交 e18f4de,2021 年 9 月 8 日)

t9001路径 不得使用 Windows 样式路径

签字人:Johannes Sixt

在 Windows 上,$(pwd) 返回驱动器盘符样式路径 C:/foo,而 $PWD 包含 POSIX 样式/c/foo 路径。
当我们想要在 PATH 变量中插入当前目录时,我们一定不能使用 C:/foo 样式,因为冒号的含义是不明确的。
使用 POSIX 风格。

/bin/pwd uses OS-specific calls to determine the current working directory.

you have an illustration of that with Git 2.34 (Q4 2021), which had to replace $(pwd) with $PWD, and explained why.

See commit f6a5af0 (24 Aug 2021) by Johannes Sixt (j6t).
(Merged by Junio C Hamano -- gitster -- in commit e18f4de, 08 Sep 2021)

t9001: PATH must not use Windows-style paths

Signed-off-by: Johannes Sixt

On Windows, $(pwd) returns a drive-letter style path C:/foo, while $PWD contains a POSIX style /c/foo path.
When we want to interpolate the current directory in the PATH variable, we must not use the C:/foo style, because the meaning of the colon is ambiguous.
Use the POSIX style.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文