Operating System - HP-UX
1753876 Members
7277 Online
108809 Solutions
New Discussion юеВ

su -c and user environment

 
SOLVED
Go to solution
Woo Kim Chye
Occasional Advisor

su -c and user environment

Hi,
I try to run a script from the root user using the su -c command. The script to be called is using the $HOME environment. However, the $HOME env is till pointing to the root user instead of the intended su user. Below is what I get when I ru the su command from the root user:

$ su wsm-dev1 -c "echo $HOME"
/root

As you can see, the $HOME is echoed to be /root instead of /home/wsm-dev1 which is the home dir for the user wsm-dev1.

Does anyone have idea on how to get the user env instead of the root user?

Thanks.
5 REPLIES 5
James R. Ferguson
Acclaimed Contributor

Re: su -c and user environment

Hi:

# su - wsm-dev1 -c "echo \$HOME"

Use the 'su -' to cause the profile of the target user to be sourced (read) as if this were a login.

Escape ('\') the HOME variable so that the shell doesn't interpret the variable before it is passed along.

Also, consider the effect of the 'SU_KEEP_ENV_VARS' if you have defined it in your '/etc/default/security' file:

http://www.docs.hp.com/en/B3921-60631/security.4.html

Regards!

...JRF...
sandeep mathur
Respected Contributor

Re: su -c and user environment

use su -wsm -dev1 -c "echo $/HOME".
su - for the profile of the target to be sourced.
and "/" for the shell is always good for use.

try this and you may get the answer.
if the answer is correct please gimme the points.
Dennis Handly
Acclaimed Contributor

Re: su -c and user environment

>sandeep: use su -wsm -dev1 -c "echo $/HOME".

This isn't helpful. There are at least 3 things wrong with it. Better to use JRF' solution.
Peter Nikitka
Honored Contributor
Solution

Re: su -c and user environment

Hi,
I'm missing the most obvious solution in preventing the expansion of the $-variable and using single quotes:
su - wsm-dev1 -c 'echo $HOME'

mfG Peter
The Universe is a pretty big place, it's bigger than anything anyone has ever dreamed of before. So if it's just us, seems like an awful waste of space, right? Jodie Foster in "Contact"
Dennis Handly
Acclaimed Contributor

Re: su -c and user environment

>Peter: I'm missing the most obvious solution in preventing the expansion of the $-variable and using single quotes

Yes, that works. I guess it depends on how many $ variable are there and which need to be replaced by the current shell and which by the su shell.