How to escape whitespace in variable passed to cd
I am using WSL on windows 10 with Ubuntu.
The path to a directory I use frequently includes a directory with a space in the name. I can't change that name.
The path to the directory is very long: /mnt/c/Users/name/Dropbox/My PC (Laptop...)/Desktop/Studies/Python
So I would like to create a variable in .bashrc by the name of $PROJECTPATH
that would be equal to the directory's path.
That way I could call or alias cd $PROJECTPATH
and get to my files.
I tried writing:
PROJECTPATH='/mnt/c/Users/name/Dropbox/My\ PC\ \(Laptop...\)/Desktop/Studies/Python'
alias prjct='cd $PROJECTPATH'
but when running prjct I'd get the error message "too many arguments" which means there's a space being passed to cd.
How could I escape whitespaces when passing a variable to cd or any other command?
And should I export this kind of variable?
You need to put double quotes around $PROJECTPATH
in your alias
definition.
Also, I recommend against backslashes in your variable definition. It's less readable and isn't needed in this case:
PROJECTPATH='/mnt/c/Users/name/Dropbox/My PC (Laptop...)/Desktop/Studies/Python'
alias prjct='cd "$PROJECTPATH"'