Take the 2-minute tour ×
Server Fault is a question and answer site for professional system and network administrators. It's 100% free, no registration required.

I'm using bash in Debian.

When I export a variable to the environment of my sub-shells it works:

aUser> EXPORTED=exported
aUser> export EXPORTED
aUser> su myUser
myUser> echo $EXPORTED
# prints 'exported'

but when I create a login shell it doesn't:

aUser> EXPORTED=exported
aUser> export EXPORTED
aUser> su myUser --login
myUser> echo $EXPORTED
# prints ''

How can I export a variable or function so that I can use it in a login shell? I need to be able to use it right after it's been defined in my parent script, so I can't put it in eg. the .profile file.

share|improve this question
    
This seems to answer it: stackoverflow.com/questions/8677504/… –  Isaac Jun 13 '13 at 8:22

1 Answer 1

up vote 2 down vote accepted

When you start a shell with --login, you are deliberately erasing all environment variables and starting with a blank slate, as if the user were logging in directly. So you can't just transfer the variable.

What you can do is to write the variable to a file and have the user's .profile source that file. Do note that this opens up a can of worms, unless you're very careful with access rights to said file.

share|improve this answer
    
Thanks; I'm doing something like that; using a file to contain the function that I need to run and copying it to a place where the user can run it. –  Ygg Jun 13 '13 at 8:58

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.