I'm attempting to get into the directory /cygdrive/c/Users/my dir/Documents
:
$ DOCS="/cygdrive/c/Users/my\ dir/Documents"
$ echo $DOCS
/cygdrive/c/Users/my\ dir/Documents
$ cd $DOCS
-bash: cd: /cygdrive/c/Users/my\: No such file or directory
$ cd /cygdrive/c/Users/my\ dir/Documents
(success)
When I manually type it in, the backspace does its escape character thing, but not when I use parameter expansion with the variable DOCS
.
I tried other variations such as no backslash.
$ DOCS=/cygdrive/c/Users/Rahman\ dir/Documents
$ echo $DOCS
/cygdrive/c/Users/my dir/Documents
$ cd $DOCS
-bash: cd: /cygdrive/c/Users/my: No such file or directory
or
$ DOCS="/cygdrive/c/Users/my dir/Documents"
$ echo $DOCS
/cygdrive/c/Users/my dir/Documents
$ cd $DOCS
-bash: cd: /cygdrive/c/Users/my: No such file or directory
The same happens for $HOME
:
$ echo $HOME
/home/my dir
cd $HOME
doesn't work either. Quotes must be put around it.
What the heck:
$ DOCS="\"/cygdrive/c/Users/my dir/Documents\""
$ echo $DOCS
"/cygdrive/c/Users/my dir/Documents"
$ cd $DOCS
-bash: cd: "/cygdrive/c/Users/my: No such file or directory
Here's your first problem. This puts an actual backslash character into
$DOCS
, as you can see by running this command:When defining
DOCS
, you do need to escape the space character. You can quote the string (using either single or double quotes) or you can escape just the space character with a backslash. You can't do both. (On most Unix-like systems, you can have a backslash in a file or directory name, though it's not a good idea. On Cygwin or Windows,\
is a directory delimiter. But I'm going to assume the actual name of the directory ismy dir
, notmy\ dir
.)This passes two arguments to
cd
. The first iscygdrive/c/Users/my\
, and the second isdir/Documents
. It happens thatcd
quietly ignores all but its first argument, which explains the error message:To set
$DOCS
to the name of yourDocuments
directory, do any one of these:Once you've done that, to change to your
Documents
directory, enclose the variable reference in double quotes (that's a good idea for any variable reference in bash, unless you're sure the value doesn't have any funny characters):You might also consider giving that directory a name without any spaces in it -- though that can be hard to do in general on Windows.
If you want to move from
c:\
and you want to go toc:\Documents and settings
, write on console:c:\Documents\[space]+tab
and cygwin will autocomplete it asc:\Documents\ and\ settings/
METHOD1: With Quotes
Method2: Without using Quotes
For the above question:
cd /cygdrive/c/Users/my\ dir/Documents
Instead of:
Try:
This should work on any POSIX system.
You need to quote
"$DOCS"
to prevent spaces from being parsed as word separators. More often than not, variable references should be quoted.Note that
$HOME
would have the same problem. The issue is coming from when the shell evaluates variable references; it's nothing to do with what variables you use or how you assign to them. It's the expansion that needs to be quoted.This is deceptive.
echo
is actually echoing the two strings/home/my
anddir
. If you usecd
orls
you'll see how it's actually working.Great question! Let's examine the commands you typed:
The reason this doesn't work is because Bash doesn't parse quotes inside variable expansions. It does perform word splitting, so whitespace in unquoted variable expansions is taken as word separators. It doesn't parse quotes in any way, meaning you can't put double quotes inside a variable to override word splitting.
Because of this,
cd
is passed two parameters. As far ascd
knows it looks like you wrote:Two parameters, with double quotes intact.
try