Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

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?

share|improve this question
    
Note that Ubuntu and CentOS (and therefore presumably Red Hat) don't have /usr/bin/cd, only the shell built-in. –  Keith Thompson Oct 5 '12 at 21:03
    
My Fedora 19 includes /bin/cd however. –  slm Feb 26 at 14:38
    
Debian (I checked several releases, including current testing) has neither /bin/cd nor /usr/bin/cd. –  derobert Feb 26 at 17:16
add comment

2 Answers

up vote 26 down vote accepted

It serves primarily as making sure the POSIX tool-chest is available both inside and outside a shell.

Also, the cd command 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.

share|improve this answer
    
+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 at 1:11
add comment

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
share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.