GNU/Linux >> Tutoriels Linux >  >> Linux

Différence étrange entre Pwd et /bin/pwd ?

J'ai ajouté un lien symbolique vers le répertoire courant avec ln -s . aa . Si j'exécute cd aa , et après cela j'ai exécuté pwd , la réponse est /home/sim/aa .

Mais si j'exécute /bin/pwd il imprime /home/sim (le répertoire courant n'a pas changé).

D'où vient cette différence ?

Réponse acceptée :

Dans la plupart des shells, y compris bash, pwd est un shell intégré :

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

Si vous utilisez /bin/pwd , vous devez utiliser le -L option pour obtenir le même résultat que pwd intégré :

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

Par défaut, /bin/pwd ignore les liens symboliques et imprime le répertoire réel.

Depuis 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.

Le pwd intégré inclut le lien symbolique par défaut, sauf que -P l'option est utilisée, ou -o physical set builtin est activé.

De 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.

Linux
  1. Différence entre /bin/false et /sbin/nologin

  2. Linux :Différence entre /dev/console , /dev/tty et /dev/tty0 ?

  3. Différence entre 2>&-, 2>/dev/null, |&, &>/dev/null et>/dev/null 2>&1 ?

  4. La différence entre /opt et /usr/local ?

  5. La différence entre '$ . Foo' Et '$ ./foo' ? ?

Linux – Fusionner /usr/bin et /usr/sbin dans /bin (gnu/linux) ?

Quelle est la différence entre #!/usr/bin/env bash et #!/usr/bin/bash ?

Quand dois-je utiliser /dev/shm/ et quand dois-je utiliser /tmp/?

Différence entre /etc/crontab et crontab -e

Quand dois-je utiliser #!/bin/bash et quand #!/bin/sh ?

Différence entre /bin et /usr/bin