2

When you type cd, it basically does the same thing as cd ~. Why?

I'm wondering if it's just a hard-coded behavior or if bash is expanding the command or something.

  • 2
    man cd: 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. – don_crissti May 8 '13 at 19:01
4

That is the POSIX specified behaviour. Not bash-specific.

  • 3
    And it's not only POSIX shells. All shells do, even non-POSIX and non-Bourne ones like csh, rc, fish... cd is one of the few builtins that is common to every shell since Unix version 7. – Stéphane Chazelas May 8 '13 at 19:55
  • @StephaneChazelas are there actually any non-POSIX shells still around? besides DOS (and probably powershell)? – strugee May 9 '13 at 0:23
  • 1
    @strugee. As I said, csh (and its derivative tcsh), rc (and it's derivatives like es or akanga), fish, and a few esoteric ones. CMD.EXE, COMMAND.COM and POWERSHELL.EXE are not Unix shells AFAIK. tcsh and fish are well alive. zsh is POSIX-like but not POSIX compliant and is the Unix shell used by most people who actually /use/ a shell (as their main interface to the system). – Stéphane Chazelas May 9 '13 at 7:14
  • @StephaneChazelas yeah, i was being more general than "unix shell" when i said shell. anyway, thanks! – strugee May 9 '13 at 17:42
  • Doesn't do that under Windows... Bites me every time. – Thorbjørn Ravn Andersen Jan 28 at 15:00

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.