87

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?

kojiro
  • 4,644

3 Answers3

83

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.

  • 6
    +1 for the observation about side effects and error messages. It is not always well explained to new users that a lot of clever idioms in Unix come from careful use of side effects. And the man pages themselves have never been good at describing the bigger picture. – RBerteig Feb 11 '14 at 01:11
  • 1
    Came here from a similar question on SO, could you elaborate what you mean when you say "automounting of a directory" ? – ffledgling Jun 29 '16 at 14:47
  • 2
    @ffledgling, see https://en.wikipedia.org/wiki/Automounter – Stéphane Chazelas Jun 29 '16 at 16:35
  • 2
    To unpack Stéphane Chazelas's thought a little: if you need to ensure that an automoutable directory is in fact mounted without making any changes on it and without making an attempt to read any files, you could use the external cd command. Might be useful in monitoring the availability of an automountable remote filesystem, or perhaps before doing something time-critical with one. – telcoM Jun 05 '18 at 13:30
  • "without making any changes on it" - It may update its access time if that option is enabled in the filesystem. – Dennis Williamson Jun 29 '22 at 17:12
14

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
jlliagre
  • 61,204
  • 3
    A number of built-ins are actually exempt from that rule. Bizarrely, cd is not one of them. – Kevin Mar 12 '16 at 07:46
  • 5
    @Kevin I wrote it is a requirement for all regular builtins, the exempted ones are special builtins: break, :, continue, . , eval, exec, exit, export, readonly, return, set, shift, times, trap, unset would be irrelevant as external commands while cd is a regular builtin that has some documented use cases as a command. – jlliagre Mar 12 '16 at 11:02
3

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.

Michael Homer
  • 76,565