Shell built in pwd against / bin / pwd

I would like to know the code implementation of the built-in pwd and / bin / pwd, especially when the directory path is a symbolic link.

Example:

hita@hita-laptop:/home$ ls -l
lrwxrwxrwx  1 root root   31 2010-06-13 15:35 my_shell -> /home/hita/shell_new/hita_shell

hita@hita-laptop:/home$ cd my_shell

hita@hita-laptop:/home/my_shell$ pwd <SHELL BUILT-IN PWD>  
/home/my_shell

hita@hita-laptop:/home/my_shell$ /bin/pwd  
/home/hita/shell_new/hita_shell

In both cases, the conclusion is different. Any clue?

thank

+1
source share
4 answers

The built-in pwd shell has the advantage that you can remember how you accessed the symbolic directory, so it shows you this information. The standalone utility just knows what your actual working directory is, and not how you changed to this directory, so it tells the real path.

, , , , , . , , , .

+2

( inode), , , (..), . "" "" . getcwd (3), ; Linux , , .

, bash, PWD, , , , . .

/bin/pwd getcwd (3), .. ; -L, PWD ( , ). ( Gnu/bin/pwd , , .)

Bash pwd , ; (.. ). pwd set -o (on) + o (off is plus!). ( ) .

# make a directory with a symlink alias
cd /tmp
mkdir real
ln -s real sym
cd sym

pwd  # will say sym
pwd -L # will say sym
pwd -P # will say real
/bin/pwd  # will say real
/bin/pwd -L # will say sym
/bin/pwd -P # will say real

rm /tmp/sym
pwd  # says sym, though link no longer exists
/bin/pwd -L # will say real!

rmdir /tmp/real
pwd  # says sym, though no directory exists
/bin/pwd # says error, as there isn't one

, , "" ; . , , , ... , , .. , :

mkdir -p /tmp/dir/subdir
ln -s /tmp/dir/subdir /tmp/a
cd /tmp/a
ls .. # shows contents of /tmp/dir
(cd .. ; ls) # shows contents of /tmp

, ~/.bashrc   set -o

, !

, J.

PS Linux Gnu bash; , .

+2

, currenct, cd ( . ..). , cd ... /bin/pwd , inodes .

+1
source

The built-in pwdshows symbolic links by default, but will not do if you give it an option -P.

In contrast, the command pwddoes not show symbolic links by default, but will do so if the option is given -L.

+1
source

Source: https://habr.com/ru/post/1778655/


All Articles