BladeSystem Forums have moved here
To make BladeSystem information easier to find, we have moved the BladeSystem forums here, to Servers and Operating Systems.
Showing results for 
Search instead for 
Do you mean 

export in a script

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
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...
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
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
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...
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
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.
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.
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.
Highlighted
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