views:

26

answers:

4

I ran the below script to set environment variables for oracle(oracle_env.sh which comes with oracle package itself).

ORACLE_HOME=/usr/lib/oracle/xe/app/oracle/product/10.2.0/server
export ORACLE_HOME
ORACLE_SID=XE
export ORACLE_SID
NLS_LANG=`$ORACLE_HOME/bin/nls_lang.sh`
export NLS_LANG
PATH=$ORACLE_HOME/bin:$PATH
export PATH
if [ $?LD_LIBRARY_PATH ]
then
        LD_LIBRARY_PATH=$ORACLE_HOME/lib:$LD_LIBRARY_PATH
else
        LD_LIBRARY_PATH=$ORACLE_HOME/lib
fi
export LD_LIBRARY_PATH

After that when I ran env to ensure that the variables are exported properly, I found no properties are exported(below is the output).

invincible:/home/invincible# /usr/lib/oracle/xe/app/oracle/product/10.2.0/server/bin/oracle_env.sh
invincible:/home/invincible# env | grep ORACLE_HOME
invincible:/home/invincible# 

Now I am not sure whether variables are exported properly.If not what I have done wrong? Please help me out. And one more thing, I am running as root.

+3  A: 

The scripts only sets the environment inside the subshell it runs in. You should source it:

# POSIX
. /usr/lib/oracle/xe/app/oracle/product/10.2.0/server/bin/oracle_env.sh

or

# bash/ksh
source /usr/lib/oracle/xe/app/oracle/product/10.2.0/server/bin/oracle_env.sh
schot
Thanks.. it worked for me.
hnm
A: 

Exporting variables only makes them available to children of the shell you export them from. There is no way of changing the environment variables in the parent shell, as you seem to be trying to do. You can change the variables in the same shell by sourcing the script using the "dot" command:

. myscript
anon
+2  A: 

I believe that when you run a script, bash forks and execs the script in a new shell instance, any exports done in the script doesn't propagate back to your parent shell.

However it seems that you can simply execute your script with:

prompt$ . /path/to/script.sh # note the period!

Example:

prompt$ echo "export FOO=foobar" > /tmp/tst
prompt$ sh /tmp/tst
prompt$ echo $FOO

prompt$ . /tmp/tst
prompt$ echo $FOO
foobar
nicomen
A: 

I believe you should use source to load that script.

source /usr/lib/oracle/xe/app/oracle/product/10.2.0/server/bin/oracle_env.sh

From man source:

source filename [arguments]

         Read  and  execute commands from filename in the current shell environment and 
return the exit

         status of the last command executed from filename.
karlphillip