Operating System - HP-UX
1833596 Members
3375 Online
110061 Solutions
New Discussion

Passing variables in su commands

 
SOLVED
Go to solution
Brahnda A. Eleazar
Occasional Advisor

Passing variables in su commands

Peace all,

I wanted to do something simple. I want to use "su - username" command to be able to run a script using that user's environment variables.

It is something like this:
su - username -c '/path/to/script $var1'

The var1 is an input from somewhere else in the script.
I keep getting parameter not set (which is $var1). So it seemed that su couldn't accept parameterized arguments?

Any input would be much appreciated.

Thanks,
=adley=
4 REPLIES 4
Steven Schweda
Honored Contributor
Solution

Re: Passing variables in su commands

Compare the results of these commands:

echo '/path/to/script $var1'

echo "/path/to/script $var1"
Brahnda A. Eleazar
Occasional Advisor

Re: Passing variables in su commands

Hi,

I've tried using single and double apostrophe for var1 as well (and a few escape attempts). I know that single usually make the $ to be literal (I used double apos everywhere else). Just on this one line that I'm stuck.

=adley=
Dennis Handly
Acclaimed Contributor

Re: Passing variables in su commands

>I've tried using single and double apostrophe for var1 as well

(These are better known as single and double quotes.)
What Steven was saying is that you should use:
su - username -c "/path/to/script $var1"

You can test with:
su - username -c "echo $var1"

>I know that single usually make the $ to be literal

Right, you don't want that, unless you want to query the environment variables in the new user's shell.
Brahnda A. Eleazar
Occasional Advisor

Re: Passing variables in su commands

Peace all,

Thanks for the posts. I managed to get it to work.

Steven, sorry about missing your point earlier as well :)

Dennis, yes, it should've been "quotes" instead of the other :)

Thanks,
=adley=