The following commands does the same thing
$ cd
$ cd $HOME
$ cd ~
$ cd /home/user
In three of them, the path is relatively specified. But how or why in the earth the simple cd
opens the user's home dir?
The following commands does the same thing
$ cd
$ cd $HOME
$ cd ~
$ cd /home/user
In three of them, the path is relatively specified. But how or why in the earth the simple cd
opens the user's home dir?
This is documented behavior in the POSIX standard:
- If no directory operand is given and the HOME environment variable is set to a non-empty value, the cd utility shall behave as if the directory named in the HOME environment variable was specified as the directory operand.
This feature comes from the Bourne shell which was introduced in Version 7 Unix in 1979. In that system, HOME
was documented as “The default argument (home directory) for the cd
command”. The Bourne shell's predecessor (the Thompson shell just printed an error message if you used chdir
with no argument (the Bourne shell also shortened the name of the command to cd
).
I don't have an authoritative source for choosing this, but why not? cd
with no argument wasn't doing anything useful, and the home directory is one that users often need to come back to. The ~
syntax didn't exist yet, so cd
was a shortcut for cd $HOME
.
Linux systems usually don't have man pages for commands that are only available as shell builtins. They are documented in each shell's manual.
man cd
butNo manual entry for cd
. Thanks – tachomi Mar 08 '16 at 23:01