bash “加冒号”(“+:”)在 shell 脚本表达式中是什么意思?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19097745/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
What does "plus colon" ("+:") mean in shell script expressions?
提问by Suzan Cioc
What does this mean?
这是什么意思?
if ${ac_cv_lib_lept_pixCreate+:} false; then :
$as_echo_n "(cached) " >&6
else
ac_check_lib_save_LIBS=$LIBS
Looks like ac_cv_lib_lept_pixCreate
is some variable, so what does +:
mean?
看起来ac_cv_lib_lept_pixCreate
是一些变量,所以是什么+:
意思?
Where to learn complete syntax of curly bracket expressions? What is the name of this syntax?
哪里可以学习大括号表达式的完整语法?这种语法的名称是什么?
回答by Henk Langeveld
In the “plus colon” ${...+:}
expression, only the +
has special meaning in the shell. The colon is just a string value in this case, so we could write that snippet as ${...+":"}
.
在“加冒号”${...+:}
表达式中,只有+
在 shell 中具有特殊含义。在这种情况下,冒号只是一个字符串值,因此我们可以将该代码段编写为${...+":"}
.
For convenience, let's pretend the variable is called var
, and consider the expression:
为方便起见,让我们假设变量被调用var
,并考虑表达式:
if ${var+:} false; then ...
If the shell variable $var
exists, the entire expression is replaced with :
, if not, it returns an empty string.
如果 shell 变量$var
存在,则将整个表达式替换为:
,如果不存在,则返回一个空字符串。
Therefore the entire expression ${var+:} false
becomes either : false
(returning true) or false
(returning false).
因此整个表达式${var+:} false
变成: false
(返回真)或false
(返回假)。
This comes down to a test for existence, which can be true even if the variable has no value assigned.
这归结为存在性测试,即使变量没有赋值也可能为真。
It is very cryptic, but as it happens, is one of the few tests for the existence of a variable that actually works in most, if not all, shells of Bourne descent.
这是非常神秘的,但碰巧的是,它是为数不多的测试变量存在的测试之一,该变量在大多数(如果不是全部)Bourne 血统的 shell 中确实有效。
Possible equivalents: (substitute any variable name here for var
)
可能的等价物:(在此处替换任何变量名var
)
if [[ ${var+"is_set"} == is_set ]]; then ...
Or, probably more portable:
或者,可能更便携:
case ${var+"IS_SET"} in IS_SET) ...;; esac
回答by nickgrim
Shell Parameter Expansion documentation for bash is here. No mention of +:
, though it does mention :+
:
bash 的 Shell 参数扩展文档在这里。没有提到+:
,虽然它确实提到:+
:
${parameter:+word}
If parameter is null or unset, nothing is substituted, otherwise the expansion of word is substituted.
${parameter:+word}
如果参数为空或未设置,则不替换任何内容,否则替换单词的扩展。
回答by Sir Athos
To illustrate what has already been said:
为了说明已经说过的内容:
Unset variable (note the blank lines as a result of some echo
commands):
未设置变量(注意某些echo
命令导致的空行):
$ unset foo
$ echo ${foo}
$ echo ${foo:+:}
$ echo ${foo+:}
Null variable:
空变量:
$ foo=""
$ echo ${foo}
$ echo ${foo:+:}
$ echo ${foo+:}
:
Non-null variable:
非空变量:
$ foo="bar"
$ echo ${foo}
bar
$ echo ${foo:+:}
:
$ echo ${foo+:}
:
回答by SriniV
Simple examples will prove
简单的例子将证明
I check for presence of a parameter TEST, if present echo "Yes" else I echo "No"
我检查是否存在参数 TEST,如果存在则回显“是”,否则我回显“否”
openvas:~$ ${TEST+:} false && echo "yes" || echo "no"
no
openvas:~$ TEST=1
openvas:~$ ${TEST+:} false && echo "yes" || echo "no"
yes
openvas:~$
If you see, the parameter TEST is evaluated and it is actually unset, so it returns false and exit the path and goes to the OR Once I set the same, and test again, it flows to the echo part (continued &&) since it returns true
如果你看到,参数 TEST 被评估,它实际上是未设置的,所以它返回 false 并退出路径并转到 OR 一旦我设置相同,并再次测试,它流向回声部分(继续&&),因为它返回真