15

I have a script that contains a couple of variables that need to be set as an environment variable

The list of variables change constantly and modifying it on my end is not an option. Any idea how I would go about doing it?

sample file foo.sh

FOO="FOOFOO"
BAR="BARBAR"
philipdotdev
  • 381
  • 3
  • 13

3 Answers3

41

There is a difference between a variable and an environment variable. If you execute . foo.sh and foo.sh contains the line FOO=value, then the variable FOO will be assigned in the current process. It is not an environment variable. To become an environment variable (and thus be available to sub-shells), it must be exported. However, shells provide an option which makes all variable assignments promote the variable to an environment variable, so if you simply do:

set -a
. foo.sh
set +a

then all variable assignments in foo.sh will be made environment variables in the current process. Note that this is not strictly true: in bash, exporting a variable makes it an environement variable in the current shell, but in other shells (dash, for example) exporting the variable does not make it an environment variable in the current shell. (It does cause it to be set it in the environment of subshells, however.) However, in the context of the shell, it does not really matter if a variable is an environment variable in the current process. If it is exported (and therefore set in the environment of any sub-processes), a variable that is not in the environment is functionally equivalent to an environment variable.

William Pursell
  • 204,365
  • 48
  • 270
  • 300
11

Are you looking for the '.' command (also spelled 'source' in bash):

source foo.sh

Since you need to export them to the environment as well, you may need to do a little extra work:

while read -r line; do
    export "$line"
done < foo.sh
chepner
  • 497,756
  • 71
  • 530
  • 681
  • I created another script bar.sh that has source foo.sh while read line; do export "$line" done < foo.sh But after running bar.sh, I still get an empty string when I try to echo $FOO. None of my variables in foo.sh show up in printenv as well – philipdotdev Feb 15 '13 at 22:30
  • 1
    You would need to `source bar.sh` as well to affect the current environment. – chepner Feb 15 '13 at 22:32
  • Also, the `while` loop is instead of `source foo.sh`; you don't need both. It's not wrong, just redundant. – chepner Feb 15 '13 at 22:33
  • Note it won't work with with any bash logic in the sourced script. – Jakub M. Nov 28 '17 at 13:27
  • @JakubM. True, although I conservatively consider that a feature. A configuration file should just contain data, not code. Between the two extremes of data-only assignments and a full blown script to source would lie a theoretical subset of `bash` that is guaranteed to terminated. [`dhall`](https://github.com/dhall-lang/dhall-lang) is a recent example of a language designed to allow limited execution in the context of a configuration file. – chepner Nov 28 '17 at 13:35
-1

If you are looking to export only variables with a common prefix, e.g.:

BAZ_FOO="FOOFOO"
BAZ_BAR="BARBAR"

You could simply do this:

. foo.sh
variable_list=()
variable_list=( $(set -o posix ; set  |\
                  grep "BAZ_"         |\
                  cut -d= -f1) )
for variable in "${variable_list[@]}"
do
  export "$variable"
done
martinec
  • 7
  • 5