When is variable in backticks expanded using Bash shell?

試著忘記壹切 提交于 2020-01-05 07:35:15

问题


If I have a variable in backticks is it expanded in the shell or in the subshell? For example:

FOO=BAR
BAZ=`[[ $FOO == BAR ]] && echo 1 || echo 0`

Is it defined when $FOO is expanded? For example does the subshell see this:

[[ $FOO == BAR ]] && echo 1 || echo 0

or this:

[[ BAR == BAR ]] && echo 1 || echo 0

回答1:


(You should really use $(...) instead of backticks. But the principle is the same.)

The command to be executed in the subshell consists of the literal characters inside the command substitution form, except for the idiosyncratic and sometimes confusing rules around backslashes inside backticks. So the variable expansion happens inside the subshell.

For example,

x=$(foo=bar && echo $foo)

will define x=bar but will not result in foo being (re-)defined in the outer shell.



来源:https://stackoverflow.com/questions/43025353/when-is-variable-in-backticks-expanded-using-bash-shell

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!