2

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.

4
  • Have you tried "triple backslash followed by one space" too?
    – 146438
    Aug 26 '19 at 20:32
  • Same problem. Gives -bash: cd: /Users/<username>/Library/Application\: No such file or directory (this time no quotes at all, just triple backslash after Application) Aug 26 '19 at 20:36
  • I wonder if triple backslash with no quotes is equivalent to single backslash with double quotes which you had tried already.
    – 146438
    Aug 26 '19 at 20:39
  • Please post this as an answer, not as an edit to the question.
    – nohillside
    Aug 27 '19 at 18:26
3

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'"
6
  • Tried cd "$ANKI_MEDIA", didn't work! Do I need to put the same line in a file titled .bashrc? Aug 27 '19 at 14:35
  • Just issuing echo $ANKI_MEDIA is fine, the part of the pathfile that contains problematic space character is printed on screen without any distortion. It's the cd command that gives problems. Why? Keeps saying that Application is not a file or folder. Aug 27 '19 at 14:39
  • found a workaround, see OP Edit 2 Aug 27 '19 at 14:56
  • @WobblyWindows "didn't work" is a bit broad, how exactly does it fail? I've edited the question and added some explanation why paths with spaces cause problems
    – nohillside
    Aug 27 '19 at 18:27
  • "didn't work" means that after I source the .bash_profile file, then 'cd' to environment variable path as indicated above, bash responds No such file or directory Aug 29 '19 at 19:48
1

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.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .