How does `cd` work?
Not to be negative, but for learning: There are a few &quot;problems&quot; with this snippet of the article:<p>----<p><pre><code>    $ which cd
    &#x2F;usr&#x2F;bin&#x2F;cd
    $ cat &#x2F;usr&#x2F;bin&#x2F;cd
    #!&#x2F;bin&#x2F;sh
    # $FreeBSD: src&#x2F;usr.bin&#x2F;alias&#x2F;generic.sh,v 1.2 2005&#x2F;10&#x2F;24 22:32:19 cperciva Exp $
    # This file is in the public domain.
    builtin `echo ${0##*&#x2F;} | tr \[:upper:] \[:lower:]` ${1+&quot;$@&quot;}
</code></pre>
Oh, bother! Reading shell scripts can be such a hassle sometimes. I know the tr command is used to translate characters. In this particular case, the second half of the command, the part after the pipe symbol, basically converts the command cd dev to CD dev. I have no idea why this is. In any case, this modified command is passed to the builtin command which is handled by the shell (Bourne shell) that we are using.<p>----<p>1. The first mistake is typing `which cd`.  `which` is a separate program that looks things up in $PATH, which may not actually be what happens when you run the command.  You should have used `type cd`:<p><pre><code>    $ type cd
    cd is a shell builtin
</code></pre>
As you discover later in the article, `cd` <i>must</i> be a shell builtin.  Which makes it a little mysterious (and interesting!) why the file &#x2F;usr&#x2F;bin&#x2F;cd exists; it won&#x27;t really do anything, try it:<p><pre><code>    $ pwd
    &#x2F;home&#x2F;lukeshu
    $ &#x2F;usr&#x2F;bin&#x2F;cd &#x2F;usr
    $ pwd
    &#x2F;home&#x2F;lukeshu
    $ # but it will print error messages
    $ &#x2F;usr&#x2F;bin&#x2F;cd &#x2F;bogus
    &#x2F;usr&#x2F;bin&#x2F;cd: line 4: cd: &#x2F;bogus: No such file or directory
</code></pre>
So, why does &#x2F;usr&#x2F;bin&#x2F;cd exist?  The comment with the CVS ID gives us a hint: It&#x27;s a common &quot;src&#x2F;usr.bin&#x2F;alias&#x2F;generic.sh&quot; that is copied (hard-linked) in to &#x2F;usr&#x2F;bin for several shell builtins ( <a href="https:&#x2F;&#x2F;github.com&#x2F;freebsd&#x2F;freebsd&#x2F;blob&#x2F;0bc1bed704cc7b7292be893f0c8c2b9f8f6a4b60&#x2F;usr.bin&#x2F;alias&#x2F;Makefile" rel="nofollow">https:&#x2F;&#x2F;github.com&#x2F;freebsd&#x2F;freebsd&#x2F;blob&#x2F;0bc1bed704cc7b7292be...</a> ).  For other builtins that don&#x27;t <i>need</i> to be builtins, it makes sense; let other programs call them with exec.  For `cd` it doesn&#x27;t make much sense though, and I&#x27;m not sure why it exists.  Is it just for consistency with other builtins, or does it serve a real purpose? IDK.<p>(edit: the short answer is &quot;POSIX says so&quot; <a href="https:&#x2F;&#x2F;github.com&#x2F;freebsd&#x2F;freebsd&#x2F;commit&#x2F;55d0b8395514ae4055e7af8e4e9812637dbcc463" rel="nofollow">https:&#x2F;&#x2F;github.com&#x2F;freebsd&#x2F;freebsd&#x2F;commit&#x2F;55d0b8395514ae4055...</a> , but <i>why</i> does POSIX say so?  See my child comment for further citation.)<p>2. The second mistake is about what `tr` is doing.  You claimed it&#x27;s converting lowercase to uppercase; but that&#x27;s backward, it&#x27;s converting uppercase to lowercase.<p>So, <i>why</i> does it convert to lowercase?  Recall that we learned that it&#x27;s the <i>same</i> script being used for all builtins.  If it weren&#x27;t literally the same file (at the cost of a few more bytes disk space), it could have just done a search&#x2F;replace within a template, having each be `builtin BUILTIN_NAME ${1+&quot;$@&quot;}`.  But they wanted to save a few bytes, and instead the script must <i>detect</i> the appropriate builtin name by translating its program path to a builtin name.  If you execvp(&quot;cd&quot;, ...), it will invoke the script with $0 set to &quot;&#x2F;usr&#x2F;bin&#x2F;cd&quot;.  If &#x2F;usr&#x2F;bin is on a case-insensitive filesystem, and you execvp(&quot;CD&quot;, ...), that will also call the script, with $0 set to &quot;&#x2F;usr&#x2F;bin&#x2F;CD&quot;.  How is it going to translate from &quot;&#x2F;usr&#x2F;bin&#x2F;CD&quot; to &quot;cd&quot;?  The ##*&#x2F; bit trims the leading directories, then the tr bit converts the remainder to lower case.<p>(as an aside: the `${1+&quot;$@&quot;}` is a little interesting too; why not just write `&quot;$@&quot;`?  &quot;$@&quot; will expand to the full list of arguments (after argv[0]).  The ${1+...} bit says to only do that expansion if the first argument exists (i.e., there are &gt;= 1 arguments).  But that should basically be happening anyway; if there are no arguments, &quot;$@&quot; should expand to a zero-length list.  IDK, perhaps a weird historical shell?)