Tugger the SLUGger!SLUG Mailing List Archives

Re: [SLUG] Environment Variables


> Don't you need to export it as well?

errr no, 

"Shell enviroment variables 101"

# Set an environment variable

  $ TOMCAT_HOME=/dev/bush
 
# It is avaliable to the current shell

  $ echo $TOMCAT_HOME
  /dev/bush

# Launch a new shell, and use the variable

  $ ksh
  $ echo $TOMCAT_HOME


# Hmmm, Opps didn't export it from the parent shell
# exit, export and try again

  $ exit
  $ export TOMCAT_HOME
  $ ksh

# Now it is available to child process

  $ echo $TOMCAT_HOME
  /dev/bush
  
# Drop back to the parent and create a simple script

  $ exit
  $ echo "MUG_HOME=/dev/muggsy ; export MUG_HOME" >tmp.sh ; 
  $ chmod +x tmp.sh
  $ ./tmp.sh

# Now lets use the new variable

  $ echo $MUG_HOME


# err oops, export doesn't work like this, the shell spawns another
# child shell to run the program, the vars are not visible to the
# parent when the child exits. Tell the shell not to be lazy and
# to run the program itself using the dot command

  $ . ./tmp.sh

# Now the variables are avaliable to the current (and subsequent
  child) shells/processes

  $ echo $MUG_HOME
  /dev/muggsy

The users profile is run as a dot command, if it wasn't then 
setting environment variables in there would be a dead waste of
time. You don't have to export them in your .profile for them
to be available to your login shell but you will need to 
export them (either in the .profile or on the command line) if
you want them to be available to any processes your shell spawns.

HTH

Pete