
When defining DOCS, you do need to escape the space character. This puts an actual backslash character into $DOCS, as you can see by running this command: $ echo "$DOCS" $ DOCS="/cygdrive/c/Users/my\ dir/Documents" Two parameters, with double quotes intact. As far as cd knows it looks like you wrote: $ cd '"/cygdrive/c/Users/my' 'dir/Documents"' $ cd $DOCSīecause of this, cd is passed two parameters. It doesn't parse quotes in any way, meaning you can't put double quotes inside a variable to override word splitting. It does perform word splitting, so whitespace in unquoted variable expansions is taken as word separators. The reason this doesn't work is because Bash doesn't parse quotes inside variable expansions. Great question! Let's examine the commands you typed: $ DOCS="\"/cygdrive/c/Users/my dir/Documents\"" Ls: cannot access dir: No such file or directoryīash: cd: /home/my: No such file or directoryĬan I ask why it works when I manually type it in but not in a variable? Ls: cannot access /home/my: No such file or directory If you use cd or ls you'll see how it's actually working. echo is actually echoing the two strings /home/my and dir. It's the expansion that needs to be quoted. 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. Note that $HOME would have the same problem. More often than not, variable references should be quoted. You need to quote "$DOCS" to prevent spaces from being parsed as word separators. bash: cd: "/cygdrive/c/Users/my: No such file or directory What the heck: $ DOCS="\"/cygdrive/c/Users/my dir/Documents\""


Or $ DOCS="/cygdrive/c/Users/my dir/Documents"Ĭd $HOME doesn't work either. bash: cd: /cygdrive/c/Users/my: No such file or directory I tried other variations such as no backslash. When I manually type it in, the backspace does its escape character thing, but not when I use parameter expansion with the variable DOCS.

bash: cd: /cygdrive/c/Users/my\: No such file or directory I'm attempting to get into the directory /cygdrive/c/Users/my dir/Documents: $ DOCS="/cygdrive/c/Users/my\ dir/Documents"
