M S
M S

Reputation: 21

linux env command does not work as expected?

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

Answers (1)

user149341
user149341

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

Related Questions