unix 内置 `pwd` 命令和它的 $PWD 环境变量有什么区别?
情况如下。 我有一个目录呼叫:- %/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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
换句话说,使用
$PWD
就足够了,因为无论如何pwd
可能不会给你更好的结果(对于更好的任何定义)。这是为什么呢?
/bin/pwd
使用特定于操作系统的调用来确定当前工作目录 - 对于 Linux,内核仅保留已解析的目录(请参阅/proc/self/cwd
>),而 shell 的 pwds 包含 shell 认为它所在的内容。In other words, using
$PWD
is sufficient, becausepwd
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./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 ofcd
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 therealpath()
function would.See
set -o physical
inbash
.您可以通过 Git 2.34(2021 年第 4 季度)进行说明,该版本必须将
$(pwd)
替换为$PWD
,并解释了原因。请参阅 提交 f6a5af0(2021 年 8 月 24 日),作者:Johannes Sixt (
j6t
)。(由 Junio C Hamano --
gitster
-- 合并于 提交 e18f4de,2021 年 9 月 8 日)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)