Search code examples
character-encodingenvironment-variablesanki

How to set environment variable in Mac 10.14.6 Mojave with 'Application Support' in pathfile?


I'm having trouble setting an environment variable that has a pathfile containing a space ' ' character.

Before you ask, I've already tried enclosing the whole pathfile within double quotes, single quotes, no quotes but escaping with backspace.

Could it be the something to do with the encoding? The variable would be:

export A_MEDIA="/Users/polo/Library/Application Support/Anki2/me/collection.media"

once I source ~/.bash_profile, I try cd $A_MEDIA (with or without quoting the name of the variable). The response is:

-bash: cd: /Users/polo/Library/Application: No such file or directory

It's as if bash didn't know how to interpret that space between 'Application' and 'Support'. It thinks the path goes from a folder named Application to a folder named Support. It just doesn't see them as a single folder name. Any help? Please?


Solution

  • Works for me so I strongly suspect what you showed us in your problem statement is not what you're actually doing. I normally use fish so this shows me setting the env var before starting bash to show that it correctly inherits the var and also setting and using it inside bash:

    12:21 macbook opencv3  ~ > set -x A_MEDIA $HOME/Library/Application\ Support/Dock/
    12:22 macbook opencv3  ~ > bash
    running .bashrc
    bash-5.0$ cd $A_MEDIA
    bash: cd: too many arguments
    bash-5.0$ cd "$A_MEDIA"
    bash-5.0$ pwd
    /Users/krader/Library/Application Support/Dock
    bash-5.0$ export B_MEDIA="$HOME/Library/Application Support/Gitter"
    bash-5.0$ cd $B_MEDIA
    bash: cd: too many arguments
    bash-5.0$ cd "$B_MEDIA"
    bash-5.0$ pwd
    /Users/krader/Library/Application Support/Gitter
    bash-5.0$ exit
    

    Note that in a POSIX shell like bash you should almost always use double-quotes around a var expansion so that if it contains whitespace the expanded value is not split on that whitespace.