Ho aggiunto un collegamento simbolico alla directory corrente con ln -s . aa
. Se eseguo cd aa
, e dopo ho eseguito pwd
, la risposta è /home/sim/aa
.
Ma se eseguo /bin/pwd
stampa /home/sim
(la directory corrente non è cambiata).
Da dove viene questa differenza?
Risposta accettata:
Nella maggior parte delle shell, inclusa bash, pwd
è una shell incorporata:
$ type -a pwd
pwd is a shell builtin
pwd is /bin/pwd
Se usi /bin/pwd
, devi usare -L
opzione per ottenere lo stesso risultato di pwd
integrato :
$ ln -s . test
$ cd test && pwd
/home/cuonglm/test
$ /bin/pwd
/home/cuonglm
$ /bin/pwd -L
/home/cuonglm/test
Per impostazione predefinita, /bin/pwd
ignora i collegamenti simbolici e stampa la directory effettiva.
Da 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.
Il pwd
integrato include il collegamento simbolico per impostazione predefinita, tranne che -P
viene utilizzata l'opzione, o -o physical
set integrato è abilitato.
Da 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.