Share your knowledge at the LQ Wiki.
Go Back > Forums > Non-*NIX Forums > Programming
User Name
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.


  Search this Thread
Old 10-11-2008, 06:49 PM   #1
LQ Newbie
Registered: Feb 2003
Location: MA
Distribution: Fedora 8; Ubuntu Jaunty
Posts: 7

Rep: Reputation: 0
bash script and exporting variables into a subshell

Hi there,
I have a bash script that I grabbed from work which checks that the user running the script is a specific user (we'll call the user foo). If the user is not foo then the current user name is exported to an environment variable and the script re-executes through sudo as foo (sudo -u foo $0 $*).

So far so good, however when the script gets re-run the environment variable no longer exists. It runs fine at work (REL, but I don't know which version), but not locally (Fedora 8).

My suspicion is that sudo as a different user is causing me to lose the environment variable, but that doesn't sound right either. Any hints?


echo "0--owner=$OWNER"
echo "0--user=$USER"
if [ "$1" = "" ]; then
    echo "Usage: foo <bar>"

if [ ! -d "$base" ]; then
    echo "Base directory '$base' does not exist"
    exit 1

if [ "$USER" != "foo" ]; then
    export OWNER=$USER
    echo "1--owner=$OWNER"
    sudo -u foo $0 $*
    echo "DONE"

echo "2--owner = $OWNER"
if [ "$OWNER" = "" ]; then

echo "3--owner = $OWNER"

<meat of the script>
Results of execution:
2--owner =
3--owner = foo

Last edited by spork; 10-11-2008 at 08:16 PM.
Old 10-11-2008, 07:42 PM   #2
LQ Guru
Registered: Aug 2004
Location: Sydney
Distribution: Centos 6.9, Centos 7.3
Posts: 17,396

Rep: Reputation: 2395Reputation: 2395Reputation: 2395Reputation: 2395Reputation: 2395Reputation: 2395Reputation: 2395Reputation: 2395Reputation: 2395Reputation: 2395Reputation: 2395
I think the env at work isn't what you think.
Its a fundamental of shells that you can only create an env var into the current env, and optionally export it to all child shells created from the current shell.
However, as soon as the 'current'/parent shell exits(!), all new env vars created are destroyed.
IOW, you can't export env vars 'upwards'.
Every time you run a shell prog it crates a new (sub) shell to run in. It doesn't run in the env its called from.
You can get env vars to remain if you source the code instead of running it normally ie

source some_shell_prog


. some_shell_prog

Old 10-11-2008, 08:15 PM   #3
LQ Newbie
Registered: Feb 2003
Location: MA
Distribution: Fedora 8; Ubuntu Jaunty
Posts: 7

Original Poster
Rep: Reputation: 0
Thanks Chris. That confirms what I was thinking.

What is odd is that it works at work. OWNER is used later on in the script and is available with the original caller's name.

I also realize I made a typo in the code section where the username in the sudo line is wrong. I will correct that now.

Thank you for the link.
Old 10-14-2008, 11:38 PM   #4
Registered: Jul 2004
Location: Rio de Janeiro - Brazil
Distribution: Conectiva 10 - Conectiva 8 - Slackware 9 - starting with LFS
Posts: 519

Rep: Reputation: 34
Hi Spork,

The sudo had recently improved its security rules. One of the more devastating things to me was the clear of the environment variables as part of this policy. You can do "man sudo" and look for "preserve environment" and "setenv".

RHEL 4 and/or 4 is perhaps a relatively old distribution and uses the old sudo behavior. Fedora 8 is the distribution I use and it's where the things are broken.



Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
set variables in a bash script; ansi PS1 color script donnied Programming 4 11-21-2007 11:33 AM
bash script, understanding variables antis Programming 5 11-15-2007 04:27 AM
Using Bash Script for Exporting and Returning Environmental Variables Jicksta Programming 3 12-04-2004 04:14 PM
bash script variables twantrd Programming 7 11-17-2004 02:38 AM
exporting variables under bash isn't working! dtheorem Programming 6 03-11-2004 08:35 AM > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 09:31 AM.

Main Menu
Write for LQ is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration