I'd like to add an environment variable for a specific account where the variable has a space. It seems that editing .bash_profile like below doesn't work:

export ENV_VAR=“/Applications/Android Studio.app“

and neither does:

export ENV_VAR=“/Applications/Android\ Studio.app“

I'm having to settle with changing the name of the application package, which isn't preferable.


export ENV_VAR="/Applications/Android Studio.app" should set the variable properly, but it's likely the program/script that uses the variable doesn't quote it properly. If if you can figure out what's using the variable, it might be possible to fix the script/program.

My first guess would be that the environment variable is used by a shell script that does something like:

somecommand $ENV_VAR/subdir/filename

The problem occurs because the shell will expand the variable, then perform word splitting -- that is, it will split /Applications/Android Studio.app/subdir/filename into two arguments: /Applications/Android and Studio.app/subdir/filename. Note that embedding escapes or quotes in the variable value doesn't do anything useful, because the shell processes quotes and escapes before performing variable substitutions. Thus, if you used export ENV_VAR="/Applications/Android\ Studio.app", it would be split into /Applications/Android\ and Studio.app/subdir/filename; if you used export ENV_VAR="'/Applications/Android Studio.app'" it would be split into '/Applications/Android and Studio.app/subdir/filename'.

The solution is to put double-quotes around the variable reference, like this:

somecommand "$ENV_VAR/subdir/filename"

which tells the shell not to perform word splitting after substitution, removing the problem.

Note that it's also possible that it's something other than a shell script, or a shell script that's doing something more complicated like using the variable to construct another variable, and possibly depending on word splitting when that variable is used. Fixing this sort of usage can get messy.

  • Ah got it. Along with that, I realised from your answer that my text editor was using curly quotation marks, instead of straight ones. All sorted!
    – andrewb
    Jun 18 '14 at 4:55

You must log in to answer this question.

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