Operating System - Linux
1753971 Members
7822 Online
108811 Solutions
New Discussion юеВ

Environment variable setup using export failed to setup env variables

 
SOLVED
Go to solution
Danny Fang
Frequent Advisor

Environment variable setup using export failed to setup env variables

HI,

I have a short script below to set some environment variables.

#!/bin/sh
export GATEWAY_ROOT=/prodkl/working/huawei_nss/V33
export GATEWAY_FRAMEWORK=/prodkl/working/GATEWAY_FRAMEWORK
export GATEWAY_CONFIG=/prodkl/working/3rd_LINE/huawei_nss/V33/GATEWAY_CONFIG
export VENDOR_GATEWAY=/prodkl/working/3rd_LINE/huawei_nss/V33/VENDOR_GATEWAY
export PERL5_BASE=/prodkl/working/3rd_LINE/PERL5_BASE
export TZ=GMT+8

However, executing the script does not seem to set the environment variables.
bash-3.00$ echo $TZ

bash-3.00$ echo $GATEWAY_CONFIG

bash-3.00$ echo $VENDOR_GATEWAY

bash-3.00$ echo $GATEWAY_ROOT

May I understand where did go wrong?

Thanks
Danny.
3 REPLIES 3
Oviwan
Honored Contributor
Solution

Re: Environment variable setup using export failed to setup env variables

Hey

just source this file but remove the #!/bin/sh line before.

then

. /path/toyourfile/filename

now you can do echo $TZ

Regards
Ivan Ferreira
Honored Contributor

Re: Environment variable setup using export failed to setup env variables

As menthined by Oviwan, the problem is that the variables are set into a new shell (#!/bin/bash), then the script finishes and the shell ends.

To avoid this, and set the variables to your current shell, remove the first line and run:

. shell.sh

This is dot, space, shell.sh. Or you can use:

source shell.sh

This will load the variables into your current shell.
Por que hacerlo dificil si es posible hacerlo facil? - Why do it the hard way, when you can do it the easy way?
James R. Ferguson
Acclaimed Contributor

Re: Environment variable setup using export failed to setup env variables

Hi Danny:

The key is to source (read) your script but not run it as a separate shell where its variables cannot be seen or propagated to parent shell.

Hence:

#!/bin/sh
echo "I am the parent script"
. myenv.sh #...NOTE the DOT then WHITESPACE followd by the file name!
echo "my GATEWAY_ROOT is ${GATEWAY_ROOT}"

This is WRONG:

...
myenv.sh
echo "my GATEWAY_ROOT is ${GATEWAY_ROOT}"
...

Regards!


...JRF...