0

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.

1

1 Answer 1

2

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.

1
  • 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, 2013 at 8:58

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .