Operating System - HP-UX
1833767 Members
2196 Online
110063 Solutions
New Discussion

SU loosing $HOME parameter

 
massimo_38
Advisor

SU loosing $HOME parameter

Hi!
Does anybody know if is it possible to retain the $HOME variable even after a su (or su -) command?

I mean:

1) I'm using the user "alpha" with home: /home/alpha
2) alpha makes "su beta"
3) I'd like that $HOME is still /home/alpha.

Same thing with $LOGNAME...can I have the same $LOGNAME even after a su or a su - ?

Thanks in advance.

PS Actually I make aplha write a file with his $HOME and Beta read this file overwriting his $HOME with the variabile in the file! ;-)
8 REPLIES 8
Jose Mosquera
Honored Contributor

Re: SU loosing $HOME parameter

Hi,

To keep your current environment use:
#su

To set environment use:
#su -


Rgds.
Michael Tully
Honored Contributor

Re: SU loosing $HOME parameter

Using:

$ su (will keep the same environment, but not retriev the env of the new user)

$ su - (will pick up env of the new user)
Anyone for a Mutiny ?
Borislav Perkov
Respected Contributor

Re: SU loosing $HOME parameter

Hi,

I think that you make mistake typing
su beta
instead
su - beta
Try with last one it will solve your problem.
Regards,
Borislav
Jose Mosquera
Honored Contributor

Re: SU loosing $HOME parameter

Hi,

Always you can force any environmental variables, i.e:

#whoami
user1
#echo $HOME
/home/user1
#su - user2
#whoami
user2
#echo $HOME
/home/user2
#export HOME=/home/user1
#echo $HOME
/home/user1
#pwd
/home/user2
#cd
#pwd
/home/user2 <-- $HOME assignament works
#export LOGNAME=user1
You can change $LOGNAME, but you never will the new user assignament.
#echo $LOGNAME
user1
#whoami
user2

Rgds.
Muthukumar_5
Honored Contributor

Re: SU loosing $HOME parameter

If you want to have that change then use su -

su - will use the profile of a particular user in the But su will try to use the situation as "The previously defined HOME and ENV environment variables are removed"

See su man page. It will give the difference between su with - and su without -

Easy to suggest when don't know about the problem!
massimo_38
Advisor

Re: SU loosing $HOME parameter

Thanks to everybody!

I knew that "SU - user" will get the new environment of the new user....but I want to become the new user while retaining the old environment.

If I make "su - user" I get the new environment and I loose the old correctly.

The solution is to export the variable (export LOGNAME, export HOME etc) but in shell script I don't know who is the firt user when I SU to the second.

I mean:

In the profile of the first user alpha I have:

su - beta

so...I become beta and I forget anything about alpha...so I can't get /home/alpha...whitout a crystal ball ;-)

Anyway, Thanks to everybody

Emanuele
Muthukumar_5
Honored Contributor

Re: SU loosing $HOME parameter

If you use su to change login from one user to another user then $HOME settings and environment variables set in the prev. will be lost.

But not the $LOGNAME parameter. It will be there as same as in the first login upto you use su -

--- su man page ---
The previously defined HOME and ENV environment variables are removed. ( on su without - )

I have tested the $LOGNAME parameter.

node1> echo $LOGNAME
user1
node1> su node2
node2> echo $LOGNAME
user1

So only $HOME variable and shell variables which was set at prev. user login.
Easy to suggest when don't know about the problem!
Jose Mosquera
Honored Contributor

Re: SU loosing $HOME parameter

Hi again,

Then save the alpha environment user's settings before any change, i.e:

#whoami
alpha
#echo $HOME $LOGNAME
/home/alpha alpha
#env > old.env
#su - beta
#echo $HOME $LOGNAME
/home/beta beta
#OLD_HOME=`grep ^HOME= old.env|awk -F"=" '{ print $2 }'`
#OLD_LOGNAME=`grep ^LOGNAME= old.env|awk -F"=" '{ print $2 }'`
#echo $OLD_HOME $OLD_LOGNAME
/home/alpha alpha
#export HOME=$OLD_HOME
#export LOGNAME=$OLD_LOGNAME
#echo $HOME $LOGNAME
/home/alpha alpha

Rgds.