cd's a builtin command in the shell.and indeed, it has to be—the script is puzzling, since to my knowledge it can't change the working directory of its parent process without using some application-specific IPC to do so, and as such, what it does is start a new shell, change that shell's current directory to the specified directory, then exit from it, returning to the parent shell, with the original working directory. Useless.
I should clarify that the first argument is the name of the script, so this is the same script that handles fg, alias and other otherwise-builtin commands - you can see that it just repeats what you put in if you copy it elsewhere, put an 'echo' in before it, and run it from the other location.Ah, okay, so it may have been linked to cd because someone thought it sensible to provide shell script versions of all the built-in commands, even where that made no sense.
#!/bin/sh
# $FreeBSD: src/usr.bin/alias/generic.sh,v 1.1 2002/07/16 22:16:03 wollman Exp $
# This file is in the public domain.
${0##*/} ${1+"$@"}
Use the "builtin" shell function to make sure that the requested
command is handled as a shell function. This avoids the following
peculiar behaviour when /usr/bin is on a case-insensitive filesystem:
# READ foo
(... long pause, depending upon the amount of swap space available ...)
sh: Resource temporarily unavailable.
$ which cdIf I edit /usr/bin/cd and add an "echo FOO" to it, when I execute "CD" (in caps), it goes into an infinite loop printing "FOO" until it runs out of processes. So it seems like it's not even preventing the problem it was meant to. Wtf.
/usr/bin/cd
$ which CD
/usr/bin/CD
$ cd
$ CD
/usr/bin/CD: fork: Resource temporarily unavailable
${1+"$@"}
${1:+"$@"}
#!/bin/sh
ruby -e 'p ARGV' ${1+"$@"}
ruby -e 'p ARGV' "$@"
posted by edd at 3:38 PM on March 22, 2007