The Straight Dope

Go Back   Straight Dope Message Board > Main > General Questions

Reply
 
Thread Tools Display Modes
  #1  
Old 01-31-2009, 04:54 PM
ticker ticker is offline
Guest
 
Join Date: Apr 2000
Unix/Linux environment variables for simpletons

I am new to the Unix/Linux world and could do with some help regarding environment variables, shell scripts and the like. As I understand it if I have the following in a shell script:
Code:
MYVAR=somevalue
export MYVAR
then $MYVAR is available within the remainder of the script and to any commands called within it, but not to the shell that called the script. This thwarted my naive plan to create a script which set up a bunch of environment variables, which could then be called from other scripts as required . Is there a simple way to achieve what I was trying to do.
Reply With Quote
Advertisements  
  #2  
Old 01-31-2009, 05:03 PM
Rysto Rysto is offline
Member
 
Join Date: Jun 2002
Posts: 5,903
What you want to do is source the script that sets the variables:

/etc/variables.conf:
Code:
MY_VAR=value
my_script.sh:
Code:
#!/bin/sh

. /etc/variables.conf

echo $MY_VAR

Run my_script.sh, and it will print "value".
Reply With Quote
  #3  
Old 01-31-2009, 05:17 PM
ticker ticker is offline
Guest
 
Join Date: Apr 2000
Simple , Cool , Thanks
Reply With Quote
  #4  
Old 01-31-2009, 05:24 PM
Digital Stimulus Digital Stimulus is offline
Guest
 
Join Date: Jul 2003
Also, be aware that various shells have different commands. For instance, export isn't universal; in csh, you would use setenv.

In addition, I'm pretty sure that not all shells recognize "." as equivalent to "source".
Reply With Quote
  #5  
Old 01-31-2009, 07:42 PM
Pasta Pasta is offline
Charter Member
 
Join Date: Sep 1999
Posts: 1,480
To elaborate...

If you have a script that you'd like to run in a new subshell (so that when it ends, your current environment is unchanged) you do what you did in the OP. If you want to run the lines of the script in your current shell, thereby modifying its environment, you have to "source" the script, like so:

contents of blah.sh:
Code:
export MYVAR="foobar"
export MYOTHERVAR=65
Then at your prompt:
Code:
myprompt> source blah.sh
Bash has an alias for "source", namely the period (.), so you could type ". blah.sh" instead of "source blah.sh". Tcsh doesn't have this. To find out what shell you are running, type "ps" and look for "bash" or "tcsh". (It is possible but unlikely that you are running a shell other than these.)

The tcsh equivalent of the above script would be:
Code:
setenv MYVAR "foobar"
setenv MYOTHERVAR 65
Reply With Quote
  #6  
Old 01-31-2009, 08:17 PM
Rysto Rysto is offline
Member
 
Join Date: Jun 2002
Posts: 5,903
Quote:
Originally Posted by Pasta View Post
Bash has an alias for "source", namely the period (.), so you could type ". blah.sh" instead of "source blah.sh".
I believe that . works in all bourne shells, but don't quote me on that,

Quote:
To find out what shell you are running, type "ps" and look for "bash" or "tcsh". (It is possible but unlikely that you are running a shell other than these.)
The $SHELL environment variable should be set to the path of the shell.
Reply With Quote
Reply

Bookmarks

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Forum Jump


All times are GMT -5. The time now is 11:34 PM.


Powered by vBulletin® Version 3.7.3
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.

Send questions for Cecil Adams to: cecil@chicagoreader.com

Send comments about this website to: webmaster@straightdope.com

Terms of Use / Privacy Policy

Advertise on the Straight Dope!
(Your direct line to thousands of the smartest, hippest people on the planet, plus a few total dipsticks.)

Publishers - interested in subscribing to the Straight Dope?
Write to: sdsubscriptions@chicagoreader.com.

Copyright © 2013 Sun-Times Media, LLC.