I have an issue setting an environment variable on my MacBook Pro running macOS High Sierra.

In the terminal, I tried to setup an environment variable by executing export VARIABLE_URL=example.com. I then tried to check that the environment variable was set with echo $VARIABLE_URL, but it returned a blank result.

Now whenever I start up the terminal, I get this message:

Last login: Tue Apr  2 15:08:56 on ttys000
[1]   Done                    export VARIABLE_URL=example.com
[2]   Done                    appVersion=
[3]   Done                    osVersion=
[4]   Done                    deviceType=
[5]+  Done                    deviceId=
My-MacBook-Pro:~ me$

I tried adding the variable to my .bash_profile file, but even this doesn't set the variable properly. Every time I do echo $VARIABLE_URL, it returns blank.

I've set other variables in my .bash_profile, but this is the only variable that my computer refuses to add.

Even when I execute printenv, all environment variables are displayed except for VARIABLE_URL.

Why can't I set this environment variable?

Edit: Here is the contents of my .bash_profile:

export BACKEND=example
export VARIABLE_URL=example.com
export HOST=localhost
export BASEURL=example
export SECRET=example
export USERNAME=example
export ACCESS_KEY=example

Edit2: Here is what is displayed when I open the terminal now:

Last login: Wed Apr  3 13:06:20 on ttys000
+ export BACKEND=example
+ BACKEND=example
+ export 'VARIABLE_URL=example.com'
+ VARIABLE_URL='example.com'
+ appVersion=
+ osVersion=
+ deviceType=
+ clientType=
+ export HOST=localhost
+ HOST=localhost
+ export BASEURL=example
+ BASEURL=example
+ export SECRET=example
+ SECRET=example
+ export USERNAME=example
+ USERNAME=example
+ export ACCESS_KEY=example
+ ACCESS_KEY=example
+ deviceId=
My-MacBook-Pro:~ me$

...and here is what is returned when printenv is executed:

My-MacBook-Pro:~ me$

Final Edit: It's been solved! Adding single quotes to the URL did the trick:

export VARIABLE_URL='example.com'

I should have explicitly explained that the URL shown in this questions is a placeholder URL, not the one that was actually in my .bash_profile. The actual URL does in fact contain multiple & characters. I'm sorry for not explaining that from the start. I feel that if I had done so, the problem would have been solved sooner. Regardless, thanks to everyone for their help and suggestions, I'm very happy to have this all sorted out now.

  • Can you share your .bash_profile content? If not perhaps just what you are setting and what works as well. – JakeGould Apr 2 at 23:07
  • The output starting with "++ shell_session_history_check" is not relevant (it's macOS's script for updating the Terminal window title bar and other housekeeping). The output from the export VARIABLE_URL=example.com line, on the other hand, is significant but doesn't make any sense at all (including the single-quotes around parts of it). Is that the actual text, or is there something other than "example.com" there? – Gordon Davisson Apr 3 at 19:36

My hypothesis:

Your actual URL uses HTTP GET method, like this:

export VARIABLE_URL=example.com?foo=bar&appVersion=&osVersion=&deviceType=&deviceId=&fizz=buzz

Where ? and & are part of the URL syntax, yet the shell interprets them differently. & makes the preceding command be run in the background. It's also a separator, so instead of one command you have multiple. The first one is

export VARIABLE_URL=example.com?foo=bar

and this value is exported as VARIABLE_URL.

Another assumption: shopt -s nullglob is active, so the ? from the GET syntax causes unquoted $VARIABLE_URL to expand to a null string in any directory where there are no files matching the pattern example.com?foo=bar.

You want

export VARIABLE_URL='example.com?foo=bar&appVersion=&osVersion=&deviceType=&deviceId=&fizz=buzz'

where single-quotes make the shell treat ? and & characters literally (double-quotes would also work with this particular example; but I don't know your actual URL, single-quoting is safer in general). Additionally


where double-quotes prevent globbing (that would otherwise occur because of ? in the variable content), regardless of nullglob and files in the current directory.

  • You did it!!! This fixed it! Thank you so much! Adding the single quotes to the URL did the trick, everything is working as it should be. Thanks again! – Brian Apr 3 at 21:29

From the output you're getting from .bash_profile, I'm guessing you have something like this in it:

export VARIABLE_URL=example.com & appVersion= & osVersion= & deviceType= & deviceId= & somethingelse=

You don't want the "&" characters there, they change the meaning in irrelevant and useless ways. You can define a bunch of environment variables in a single export command by separating them with spaces:

export VARIABLE_URL=example.com appVersion= osVersion= deviceType= deviceId= somethingelse=

Or it might be clearer to just use a separate export command for each variable:

export VARIABLE_URL=example.com
export appVersion=
export osVersion=
export deviceType=
export deviceId=
export somethingelse=

BTW, the reason "&" causes trouble is that it's a delimiter between commands, and makes the command before it run in the background. So each assignment is treated as a completely separate command (only the first is an export, the rest are unexported shell variables), and all but the last run in the background in separate processes, and the definitions are local to those processes (and vanish when those processes finish). That's what all those "Done" lines are -- they indicate that those background processes have finished.

  • Thanks for your help! Unfortunately my .bash_profile does not contain any & symbols. I'll update my question so that you can see the contents of the file. Also, my terminal is still showing that odd message whenever it is executed. – Brian Apr 3 at 18:43
  • @Brian In that case, I'm really confused. Try adding set -x at the beginning of your .bash_profile, so the shell will print what it thinks it's executing. That may give some insight into what's going on. Oh, one other thing: have you anonymized the content of the .bash_profile, and maybe removed some metacharacters in the real content? – Gordon Davisson Apr 3 at 19:04
  • Ok, I added set -x to the beginning of the .bash_profile file. printenv still shows all environments except VARIABLE_URL. Is there a command I should execute to trigger the set -x line? – Brian Apr 3 at 19:11
  • Nevermind, I closed the Terminal and re-opened it. Now I'm greeted with a very long list. A similar list is shown when I run printenv. I'll include as much of it as I can in my original question. – Brian Apr 3 at 19:16
  • While not true in this case, the & is a decent assessment. – JakeGould Apr 3 at 19:35

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.