POSIX – What is the Point of the ‘cd’ External Command?

cd-commandcommandposix

As referenced in this fine answer, POSIX systems have an external binary cd in addition to the shell builtin. On OS X 10.8 it's /usr/bin/cd. You can't use it like the builtin cd since it exits immediately after changing its own working directory. What purpose does it serve?

Best Answer

It serves primarily as making sure the POSIX tool-chest is available both inside and outside a shell (see the POSIX rationale for requiring those).

For cd, that is not tremendously useful but note that cd changes directories but has other side effects: it returns an exit status that helps determine whether you're able to chdir() to that directory or not, and outputs a useful error message explaining why you can't chdir() when you can't.

Example:

dirs_i_am_able_to_cd_into=$(find . -type d -exec cd {} \; -print)

Another potential side-effect is the automounting of a directory.

On a few systems, most of the external commands for the standard shell builtins are implemented as a symlink to the same script that does:

#! /bin/sh -
"${0##*/}" "$@"

That is start a shell and run the builtin in it.

Some other systems (like GNU), have utilities as true executable commands which can lead to confusions when the behavior differs from the shell builtin version.