Global environment variables in a shell script

Alex picture Alex · Sep 23, 2009 · Viewed 142.2k times · Source

How to set a global environment variable in a bash script?

If I do stuff like

#!/bin/bash
FOO=bar

...or

#!/bin/bash
export FOO=bar

...the vars seem to stay in the local context, whereas I'd like to keep using them after the script has finished executing.

Answer

mob picture mob · Sep 23, 2009

Run your script with .

. myscript.sh

This will run the script in the current shell environment.

export governs which variables will be available to new processes, so if you say

FOO=1
export BAR=2
./runScript.sh

then $BAR will be available in the environment of runScript.sh, but $FOO will not.