简体   繁体   中英

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?

$ 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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM