Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Export <variable> is not working

Status
Not open for further replies.

CompGuy2

Programmer
Dec 6, 2001
1
When running a Bourne shell, I create a variable, display the variable, then export the variable. The display works OK, but the export does not. Why?

Here is the contents of myshell.sh

myVar="this is a test"
echo "The contents of myVar: $myVar"
export $myVar

Here is what happens when I run the shell:
$sh myshell.sh
The contents of myVar: this is a test
$echo $myVar

$

I am able to edit my .profile and create/export a variable, then successfully echo the variable from the command line. Why doesn't export work from my shell?

Thanks,
Brian
 
When you export a variable from your .profile, your shell "inherits" it.

When your run a script, a new shell is spawned, which holds the variable exported in the script. Once the script ends, the child shell is terminated. This returns control to the parent shell, which knows nothing of what variables were set by the child X-)

To force your shell to store the variable(s), you'll have to 'dot it'.

I.e., run . ./script
One by one, the penguins steal my sanity.
 
It sounds a bit of an anomoly, but 'export' does not actually export outside of the shell you are running. Therefore when your script has ended, you lose the variables even if they are exported. However if you call other scripts from within your script, then the variables will be exported to them.
 
Use
Code:
export myVar
instead of
Code:
export $myVar
, and the variable will be available to successive shells (export by name, not contents).
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top