Search code examples
bashshellsu

How to define environment variable in input command of su


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?


Solution

  • 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'