I want to run a program in Terminal and set its environment variables. According to my research one can do this using:

VARIABLE="value" VARIABLE2="value2" run_program

Now I'd like to set a variety of variables using a configuration file. Indeed, I could inject variables into the active shell using:

. program_variables.config

I then can use echo $VARIABLE in the terminal.

Anyway, when I now run

. program_variables.config run_program

I cannot access the variables within the program. I also tried separating the two parts with ; and &&.

After thinking about what's happening there I had the following ideas:

cat program_variables.config | xargs run_program
cat program_variables.config | tr -d '\n' run_program

and a few more.

I didn't write the called program myself so I cannot change the behavior how variables are read.

Any ideas how I output the file as if I would have written its content in the shell myself?


The variables are set only in the current scope, to make them visible in a program that is called or after a source (e.g. . file) you need to export them.

Thus program_variables.config should be

export VARIABLE="value" 
export VARIABLE2="value2"

Then just run the executable,

This can all be in one script to run the executable

export VARIABLE="value" 
export VARIABLE2="value2"

Save this file and make it executable.
Then just use the name os this file as the program

| improve this answer | |
  • I cannot put everything in a shell script, because the config file path is a variable itself and everything is called using a CI. But adding extern to the variables enables me to use . program_variables.config && run_program which is perfect for my needs. – Julian F. Weinert Apr 2 '15 at 23:29

I was facing the same problem, my need was simple which was to set my credentials as environment variables only for the current session of my cli so I did put all my secrets in a shell script and tried to ran it but none of my environment variables ever showed up in my echo statement, below are the steps i followed

  1. Create a simple my_credentials.sh file
  2. Export all the environment variables in there like export VARNAME="value"
  3. Save the file (you can use any editor of your choice)
  4. Open terminal and launch your shell script
  5. Try to do echo $VARNAME and my value wasn't printed because I was doing a silly mistake (I don't know why though)

Wrong Command: sh /Users/me/myShellDir/my_shell.sh
Correct Way : . /Users/me/myShellDir/my_shell.sh**

| improve this answer | |

You must log in to answer this question.

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