What is the point of the `cd` external command?
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.
The fact a non builtin cd command is available is essentially due to the POSIX requirement for all regular builtins to be callable by the exec family commands env
, find
, nice
, nohup
, time
and xargs
combined to the fact some of these commands are not being themselves implemented as builtins.
That doesn't make much sense for cd
though as combining it with these commands is quite pointless. Here are more or less tenable examples though:
find . -type d -exec cd {} \;
env HOME=/foo cd
In addition to checking whether a path corresponds to an accessible directory, the cd
executable will process and use the CDPATH
variable, and will print out the absolute path of the resolved directory if it was used successfully.
$ export CDPATH=/usr
$ echo bin lib | xargs -n 1 cd
/usr/bin
/usr/lib
This is only very occasionally useful, but would save reimplementing the same logic for searching for matching directories. A concrete use case is finding the first existing directory of a particular name under several possible parents.
cd
also processes OLDPWD
for cd -
, but that is less concretely useful since the environment variable would already be available.