Writing my first script so I'm sure this is a basic question, but can someone please explain to me why I can:
cd ~
cd bin
cd ~/bin
cd 'bin'
But not
cd '~'
cd '~/bin'
I need to cd
to a directory path with a space in one of the directory names, so I need the quotes (it's Windows Program Files
under wine). I can get around it with two cd
commands, but why can't I put ~
in quotes?
If I type cd '~'
(or cd "~"
) I get:
bash: cd: ~: No such file or directory
As @karel noted in his answer,
~
is a special character and expanded by Bash into the current user's home directory. See Bash's manual on "Tilde Expansion", or search for the headline "Tilde Expansion" in the man page (man bash
).Any kind of quotation around the
~
prevents this tilde expansion.To answer your question about how you still can use it to
cd
into a directory with spaces in its name, there are a few alternatives:Omit quotes and escape the spaces with backslashes instead:
Quote the rest of the path, but omit them around the tilde and the first slash:
As you see, you can concatenate quoted and unquoted strings in Bash by simply writing them next to each other without any spaces in between.
Use the environment variable
$HOME
instead of the tilde, which still gets expanded inside "double quotes" (but not 'single quotes'):~ is a special character that is interpreted by the shell as the logged in user's home directory. '~' is interpreted by the shell as a literal ~ character, not as the logged in user's home directory because enclosing a string inside two single quote characters results in that string being interpreted as a literal text string.
This is a
bash
feature called Tilde Expansion. Citingman bash
:For the expansion to work the tilde character
~
needs to be unquoted, else the character is taken literally andcd
fails if there is no directory named~
present in the current directory. See this entensive answer for an explanation of quoting inbash
. If you need to quote part of the path, you can therefore:quote at least the characters that need quoting with single quotes, e.g.
or
quote at least the characters that need quoting with double quotes, e.g.
or
quote only the characters that need quoting with backslashes , e.g.
Tilde Expansion has some more interesting features, e.g.:
~+
expands to the value ofPWD
, i.e. the current working directory~-
expands to the value ofOLDPWD
, i.e. the previous working directory~john
expands to the home directory associated with the login name “john”Explore using
echo
commandThe easiest way of exploring how things work in bash is with the
echo
command. In the case of~
use this:As you can see, when you single quote or use double quotes around
~
it is interpreted literally as a string and not expanded as a variable. When you use backticks (`) it is executed as a command and generates an error message.