0

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?

2

This is documented behavior in the POSIX standard:

  1. 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.
  • Oh ok, simple as that... I was trying to man cd but No manual entry for cd. Thanks – tachomi Mar 8 '16 at 23:01
1

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.

  • Outstanding your historical explanation. Thank you very much. – tachomi Mar 9 '16 at 14:24

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.