2

These 2 lines work as expected:

$ env NEW=hello bash -c "env | grep hello"
NEW=hello
$ env NEW=hello bash -c "echo $PATH"
/bin:/usr/bin

But I don't know why the following does not work (as expected).

$ env NEW=hello bash -c "echo $NEW"

Any suggestion?

Mathemats
  • 1,185
  • 4
  • 22
  • 35
M S
  • 21
  • 2

1 Answers1

1
$ env NEW=hello bash -c "echo $NEW"

You're using double-quotes on the argument to bash here, so the $NEW in the argument is expanded by your current shell, not by the bash command you're executing. Since $NEW isn't set in your current shell, the command is expanded to bash -c "echo ".

Use single-quotes on the argument to solve this:

$ env NEW=hello bash -c 'echo $NEW'
hello