Escape space character in pathfile?
I'm trying to set an environment variable for Anki's media folder, in .bash_profile
.
The line is export ANKI_MEDIA='/Users/<username>/Library/Application Support/Anki2/me/collection.media'
.
Once sourced, I try echo $ANKI_MEDIA
which correctly returns the full path but if I do cd $ANKI_MEDIA
I get back -bash: cd: "/Users/<username>/Library/Application\: No such file or directory
I've tried different combos of single and double quotes -- to no avail. Please advice, thank you in advance.
EDIT 1
I've tried writing the pathfile in .bash_profile within backticks. After sourcing it, I immediate get back -bash: /Users/<username>/Library/Application Support/Anki2/me/collection.media: is a directory
EDIT 2
For future reference, I created a link in the home directory of the User and pointed it to the collection.media
folder. This just a workaround, I would greatly appreciate it if someone helped me solve this puzzle.
The shell uses a space character as a separator between words, so paths containing spaces always must be put in ""
when used (which implies that it is recommended/best practice to ensure that all paths and file names in shell scripts and init files are properly quoted):
ANKI_MEDIA="/Users/<username>/Library/Application\ Support/Anki2/me/collection.media"
cd "$ANKI_MEDIA"
You can also use \
to protect a space but this only works for literal paths, not for variables:
cd /Users/<username>/Library/Application\ Support/Anki2/me/collection.media
If applying quotes each time you change into this directory is inconvenient you can use an alias instead:
alias cdanki="cd '$ANKI_MEDIA'"
There was a mistake on the pathfile name (after Anki2/me/...) : the user profile was actually "me_201908". Also, this time around I edited the file in vim and used $HOME in the variable's pathfile.