Reputation: 21
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?
Upvotes: 2
Views: 1738
Reputation:
$ 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
Upvotes: 1