Search code examples
linuxbashshellcd

Linux which cd worked. But cd is a builtin command in bash


I just learned Linux, and read the book "The Linux Command-Line". It says:

cd is a builtin command in bash.

So we cannot find anything with which cd; but somehow it worked well in my computer:

$ which cd
/usr/bin/cd

It is because I use CentOS?


Solution

  • For shell builtins use help rather than man. help cd will give you usage information. which is misleading since it only finds binaries. Use type.

    $ type cd
    cd is a shell builtin
    
    $ help cd
    cd: cd [-L|[-P [-e]] [-@]] [dir]
        Change the shell working directory.
    ...
    

    Now as it happens, there is a useless binary* /usr/bin/cd on your system. It's useless both because the shell builtin supercedes it, and because it's impossible for a binary to change the directory of the parent shell. Try to use it and you'll find it does nothing at all.

    /dir1$ /usr/bin/cd /dir2
    /dir1$