1847583 Members
3212 Online
110265 Solutions
New Discussion

export variable from su

 
Steve_3
Frequent Advisor

export variable from su

Problem:

Trying to export a variable from su in a script.

script:
...
...
su - oracle -c "
OWN=`ls -l | wc -l`
export $OWN
echo $OWN
"

echo $OWN

I ran it in debug mode, I see the value for OWN, but when echo there is no value.

Any suggestion???

Thanks,
steve



3 REPLIES 3
Steven Sim Kok Leong
Honored Contributor

Re: export variable from su

Hi,

It should be

export OWN

instead of

export $OWN

The dollar sign should not be present.

Hope this helps. Regards.

Steven Sim Kok Leong
Brainbench MVP for Unix Admin
http://www.brainbench.com
Steve_3
Frequent Advisor

Re: export variable from su

sorry there is no $ sign on export OWN

steve
Volker Borowski
Honored Contributor

Re: export variable from su

Hi Steve,
I think this will never work, as "su" is creating a complete new user context, wich will be destroyed after su finishes.

you'll need to go by a file:
inside the "su" do:
OWN=value
echo export OWN=$OWN > /tmp/su_env.sh
chmod 600 /tmp/su_env.sh

and later on in your script
. /tmp/su_env.sh

This should work
Volker