Lolly
Lolly

Reputation: 36372

Shell script for setting environment variable

I am writing a shell script to set the environment variables whose values are available in a file. Below is the shell script I wrote,

VARIABLE_FILE=env-var.dat
if [ -f ${VARIABLE_FILE} ] ; then
   . ${VARIABLE_FILE}
   if [ ! -z "${TEST_VAR1}" ] ; then
      export TEST_VAR1="${TEST_VAR1}"
   fi
   if [ ! -z "${TEST_VAR2}" ] ; then
      export TEST_VAR2="${TEST_VAR2}"
   fi
fi

The above code works only in bash shell, since I have used export command to set the environment variable and it fails if I used it with any other shell. Is there is any command to set the environment variable which works in any shell ?

Upvotes: 2

Views: 7169

Answers (2)

BenPen
BenPen

Reputation: 392

If you really want this to happen, it can be done, but it's tricky. One way to do it is to use awk to output the correct syntax and evaluate the text coming back from awk. To share a single environment variable value file between major sh and csh flavors, the following command in a file will import a variable value file to the environment: (yes, yes, it's one huge line, due to the inflexible way that some shells treat the backticks. If you didn't mind having a .awk file too, you could use awk -f...)

eval `awk '{ var = $1; $1="";  val=substr($0,2); if ( ENVIRON["SHELL"] ~ /csh$/) { print "setenv", var, " \"" val "\";" } else { print var "=\"" val "\"" ; print "export", var }}' $HOME/env_value_file`

The variable value file is in this format:

FOO value for foo
BAR foo bar
BAZ $BAR plus values $FOO

Design notes for educational purposes:

  1. In awk, there's no easy way of accessing fields 2-NF, so if there could be spaces in our variable values we need to modify $1 to get $0 to be close to get the value we want.
  2. To get this to work, since a SHELL variable is always set, but not as an environment variable and not with a consistent capitalization, you have to wet a SHELL environment variable from the shell's value as below. as an environment variable before you use the script.
  3. Also, if you want the new environment values to be present after the import environment script you need to source the environment script.
  4. If a shell doesn't do eval well, you'll have to tweak the script.

For bourne shell flavors (bash, sh, ksh, zsh):

export SHELL
. import_environment

For csh flavors: (shell variable tends to be lower case in csh shells)

setenv SHELL "$shell"
source import_environment

Upvotes: 0

Celada
Celada

Reputation: 22251

"Fancier" shells like bash and zsh permit you to set a variable and export it as an environment variable at the same time like so:

export FOO=bar

With a standard POSIX bourne shell, the equivalent is achieved by doing it in two commands:

FOO=bar
export FOO

Note that once you've exported a variable, you can reset it to a different value later in the script and it's still exported (you don't need to export it again). Also, you can export several variables at a time:

FOO=bar
BAZ=quux
export FOO BAZ

You mentioned tcsh in your comment, but csh and derivatives are completely different from bourne-based shells (and not recommended for use!). You can rarely make a shell script compatible with both sh and csh at the same time. For csh, look into setenv

Upvotes: 3

Related Questions