pwdと/bin/pwdの奇妙な違い

pwdと/bin/pwdの奇妙な違い

現在のディレクトリにシンボリックリンクを追加しましたln -s . aa。を実行しcd aaてから実行すると、pwd応答はです/home/sim/aa

しかし、実行すると/bin/pwd印刷されます/home/sim(現在のディレクトリは変更されていません)。

この違いはどこから来るのか?

ベストアンサー1

Bashを含むほとんどのシェルには組み込みpwdシェルがあります。

$ type -a pwd
pwd is a shell builtin
pwd is /bin/pwd

使用している場合、組み込みと同じ結果を得るには/bin/pwdこの-Lオプションを使用する必要がありますpwd

$ ln -s . test
$ cd test && pwd
/home/cuonglm/test
$ /bin/pwd
/home/cuonglm
$ /bin/pwd -L
/home/cuonglm/test

デフォルトでは、/bin/pwdシンボリックリンクは無視され、物理ディレクトリが印刷されます。

~からinfo pwd:

`-L'
`--logical'
     If the contents of the environment variable `PWD' provide an
     absolute name of the current directory with no `.' or `..'
     components, but possibly with symbolic links, then output those
     contents.  Otherwise, fall back to default `-P' handling.

`-P'
`--physical'
     Print a fully resolved name for the current directory.  That is,
     all components of the printed name will be actual directory
     names--none will be symbolic links.

pwdデフォルトでは、組み込み機能には-Pこのオプションを使用しない場合、または組み込み-o physical機能設定が有効になっていない限り、シンボリックリンクが含まれます。

~からman bash:

pwd [-LP]
              Print the absolute pathname of the  current  working  directory.
              The pathname printed contains no symbolic links if the -P option
              is supplied or the -o physical option to the set builtin command
              is  enabled.  If the -L option is used, the pathname printed may
              contain symbolic links.  The return status is 0 unless an  error
              occurs  while  reading  the  name of the current directory or an
              invalid option is supplied.

おすすめ記事