What cd am I using?
If you're in Bash cd
is a builtin. The type command even bears this out:
$ type -a cd
cd is a shell builtin
cd is /usr/bin/cd
cd is /bin/cd
The system will use the first thing in this list, so the builtin will be the preferred option, and the only one that works (see the section below on What is /bin/cd).
What's a builtin?
I like to think of builtins as functions that Bash knows how to do itself. Basically anything that you use a lot has been moved into the Bash "kernel" so that it doesn't have to go executing a process for each time.
You can always explicitly tell Bash that you want a builtin by using the builtin
command like so:
$ builtin cd
See the help about builtin
:
$ help builtin
Why isn't cd in hash?
The hash is meant only to "hash" (aka. "save" in a key/value pair) the locations of files, not for builtins or keywords. The primary task for hash
is in saving on having to go through the $PATH
each time looking for frequently used executables.
Keywords?
These are typically the commands that are part of Bash's programming language features.
$ type while
while is a shell keyword
$ type for
for is a shell keyword
$ type !
! is a shell keyword
Some things are implemented in multiple ways, such as [
:
$ type -a [
[ is a shell builtin
[ is /usr/bin/[
[ is /bin/[
...and cd
as you've discovered.
What is /bin/cd?
On my Fedora 19 system /bin/cd
is actually a shell script:
$ more /bin/cd
#!/bin/sh
builtin cd "$@"
But it doesn't do what you think. See these other U&L Q&A's for more details:
Bottom line:
POSIX's requires that it's there and in this implementation, it acts as a test, confirming that you're able to change directories to X, but then returning a return code confirming or denying that this is possible.
cd
andhash
are internal to the shell because those commands need to access internal data of the shell process or change its status. – Fernando Feb 26 '14 at 04:17cd
external command, of very limited utility. – Gilles 'SO- stop being evil' Feb 26 '14 at 10:12