tags:

views:

25

answers:

2

This command has an empty output.

su user -c "ABC=abc;echo $ABC"

Any idea, how can I define a variable in the input command?

+1  A: 

If using a bourne shell variant:

ABC=abc su user -c 'echo $ABC'

If not, use env.

env ABC=abc su user -c 'echo $ABC'
William Pursell
Thanks for the answer, it works but it is not exactly I need. I have a quite long input string for the su command and I have to determine the value of the variable as "user".
Attila Zobolyak
+1  A: 

Change your quotes to single quotes. The double quotes allow the variable to be substituted in the current environment where it's not set yet. To see the difference, try your version with $USER and compare it to this one:

su user -c 'ABC=abc; echo $ABC; echo $USER'
Dennis Williamson