1748065 Members
5389 Online
108758 Solutions
New Discussion юеВ

export in a script

 
Praveen Bezawada
Respected Contributor

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
9 REPLIES 9
James R. Ferguson
Acclaimed Contributor

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...
Andreas Voss
Honored Contributor

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
Andreas Voss
Honored Contributor

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
James R. Ferguson
Acclaimed Contributor

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...
Bruce Regittko_1
Esteemed Contributor

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
Kevin Ernst
Regular Advisor

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.
Lawrence Mahan
Frequent Advisor

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.
Lawrence Mahan
Frequent Advisor

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.
Bruce Regittko_1
Esteemed Contributor

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