Re: export in a script (244 Views)
Reply
Respected Contributor
Praveen Bezawada
Posts: 310
Registered: ‎09-13-2000
Message 1 of 10 (244 Views)

export in a script

Hi
When i export a variable in a script, and try to use it from command prompt it does not seem to work.
ex:- in file a.sh
export PRA=XXX
echo $PRA

execute a.sh
then try to do echo from prompt, the value is not set.
What should I do set the value.

please help
Praveen
Please use plain text.
Acclaimed Contributor
James R. Ferguson
Posts: 21,184
Registered: ‎07-06-2000
Message 2 of 10 (244 Views)

Re: export in a script

Hi:

The is expected. The variable is local to your script (environment). Only children of your script or inherit the variable.

...JRF...
Please use plain text.
Honored Contributor
Andreas Voss
Posts: 1,364
Registered: ‎10-14-1997
Message 3 of 10 (244 Views)

Re: export in a script

Hi,

this is normal behaviour.
export in a script is not 'back' exported.
When you start a script it executes in a sub shell that terminates on ending the script.
To have the variables in your own shell 'dot' the script:
. a.sh
then your script will not run in a subshell.

Regards
Please use plain text.
Honored Contributor
Andreas Voss
Posts: 1,364
Registered: ‎10-14-1997
Message 4 of 10 (244 Views)

Re: export in a script

Hi,

if you want shotcuts for some commands you better use the alias function. ie:
alias cdhome="cd /home"

Regards
Please use plain text.
Acclaimed Contributor
James R. Ferguson
Posts: 21,184
Registered: ‎07-06-2000
Message 5 of 10 (244 Views)

Re: export in a script

Hi:

In addition to sourcing another file, you can create and export variables you need in your $HOME/.profile. This is often done with for Oracle user profiles.

...JRF...
Please use plain text.
Esteemed Contributor
Bruce Regittko_1
Posts: 273
Registered: ‎11-15-2000
Message 6 of 10 (244 Views)

Re: export in a script

As has already been mentioned, a child cannot set an environment variable for the parent. What you can do, however, is save the value in a file and read the file from the parent. For example, in a.sh,

echo "Value" > valueFile

and then set the value from the command prompt by using command substitution,

var=$( cat valueFile )
www.stratech.com/training
Please use plain text.
Regular Advisor
Kevin Ernst
Posts: 91
Registered: ‎07-27-2000
Message 7 of 10 (244 Views)

Re: export in a script

I once thought it might be possible to place variables in the calling environment with some 'exec' trickery. But 'export' (or 'declare -x') are shell built-ins (they don't exist anywhere as regular executables), so you can't 'exec' them.
Please use plain text.
Frequent Advisor
Lawrence Mahan
Posts: 47
Registered: ‎11-01-1996
Message 8 of 10 (244 Views)

Re: export in a script

If the only thing the script is doing is seting env variable you can profile the script. Set the execute bit on the file then enter this:

. ab.sh

This will treat the file like .profile and then you will retain the variables.
Please use plain text.
Frequent Advisor
Lawrence Mahan
Posts: 47
Registered: ‎11-01-1996
Message 9 of 10 (244 Views)

Re: export in a script

If the only thing the script is doing is seting env. variables then run the script as a .profile script. Set the execute bit on the script then enter this:

. ab.sh

NOTE THE DOT

THis will treat the script as a .profile. THe variables should then take properly.
Please use plain text.
Esteemed Contributor
Bruce Regittko_1
Posts: 273
Registered: ‎11-15-2000
Message 10 of 10 (244 Views)

Re: export in a script

Actually Larry,

I don't think the execute bit even needs to be set when a file is sourced. The PATH variable will be referenced, however, if a path is not specified on the command line.

www.stratech.com/training
Please use plain text.
The opinions expressed above are the personal opinions of the authors, not of HP. By using this site, you accept the Terms of Use and Rules of Participation