什么时候使用Bash shell扩展反引号中的变量? [英] When is variable in backticks expanded using Bash shell?

查看:745
本文介绍了什么时候使用Bash shell扩展反引号中的变量?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我在反引号中有一个变量,它是在shell还是在subshel​​l中扩展?例如:

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`

是否在 $ FOO 展开了吗?例如,子外壳程序是否看到以下内容:

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

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

或此:

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


推荐答案

(您应该真正使用 $(...)而不是反引号。但是原理是一样的。)

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

要在子shell中执行的命令由命令替换形式中的文字字符组成,除了反引号内反斜杠周围的特有且有时令人困惑的规则外。因此,变量展开发生在子外壳内部。

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.

例如,

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

将定义 x = bar ,但不会导致 foo 在外壳中(重新)定义。

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

这篇关于什么时候使用Bash shell扩展反引号中的变量?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆