How to define environment variable in input command of su
Asked Answered
L

2

6

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?

Lanfri answered 9/9, 2010 at 12:34 Comment(0)
C
5

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'
Coagulant answered 9/9, 2010 at 13:29 Comment(0)
E
2

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'
Eldwen answered 9/9, 2010 at 12:49 Comment(1)
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".Lanfri

© 2022 - 2024 — McMap. All rights reserved.