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.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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
1  
My Fedora 19 includes /bin/cd however. – slm Feb 26 '14 at 14:38
    
Debian (I checked several releases, including current testing) has neither /bin/cd nor /usr/bin/cd. – derobert Feb 26 '14 at 17:16
1  
I guess that makes these systems not strictly POSIX. Just like everyone else. :) – kojiro Apr 19 '14 at 15:39
up vote 38 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 '14 at 1:11
    
Came here from a similar question on SO, could you elaborate what you mean when you say "automounting of a directory" ? – ffledgling yesterday
    
@ffledgling, see en.wikipedia.org/wiki/Automounter – Stéphane Chazelas yesterday

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
    
A number of built-ins are actually exempt from that rule. Bizarrely, cd is not one of them. – Kevin Mar 12 at 7:46
1  
@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 at 11:02

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.