string 变量替换中的 Bash 冒号运算符?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15143863/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 01:49:03  来源:igfitidea点击:

Bash colon operator in variable substitution?

stringbashvariablescolonvariable-subsitution

提问by Gardner Bickford

I inherited some bash code and these two lines are bewildering me:

我继承了一些 bash 代码,这两行代码让我感到困惑:

branch_name=`git describe --contains --all HEAD`
branch_name=${branch_name:-HEAD}

My understanding of the :colon operator is that is creates a substring based on an index so using a string, -HEADin this case, does not make any sense.

我对:冒号运算符的理解是基于索引创建子字符串,因此-HEAD在这种情况下使用字符串没有任何意义。

回答by Olaf Dietsche

This takes the variable branch_name, if it is defined. If it is not defined, use HEADinstead.

这需要变量branch_name,如果它被定义了。如果未定义,请HEAD改用。

See Shell Parameter Expansionfor details:

详见外壳参数扩展

3.5.3 Shell Parameter Expansion

The ‘$' character introduces parameter expansion, command substitution, or arithmetic expansion. ... The basic form of parameter expansion is ${parameter}.
...
When not performing substring expansion, using the form described below (e.g., ‘:-'), Bash tests for a parameter that is unset or null. Omitting the colon results in a test only for a parameter that is unset. Put another way, if the colon is included, the operator tests for both parameter's existence and that its value is not null; if the colon is omitted, the operator tests only for existence.

${parameter:-word}

If parameter is unset or null, the expansion of wordis substituted. Otherwise, the value of parameteris substituted.

3.5.3 Shell 参数扩展

'$' 字符引入了参数扩展、命令替换或算术扩展。... 参数扩展的基本形式是 ${parameter}。
...
当不执行子字符串扩展时,使用下面描述的形式(例如,':-'),Bash 测试未设置或为空的参数。省略冒号会导致仅对未设置的参数进行测试。换句话说,如果包含冒号,操作符会测试两个参数是否存在,并且它的值不为空;如果省略冒号,则运算符仅测试是否存在。

${参数:-word}

如果参数未设置或为空,word则替换的扩展。否则,将parameter替换的值。



Substrings are covered a few lines below. The difference between the two is

下面几行涵盖了子字符串。两者的区别在于

${parameter:-word}

vs

对比

${parameter:offset}
${parameter:offset:length}

${parameter:offset}
${parameter:offset:length}

This is referred to as Substring Expansion. It expands to up to length characters of the value of parameter starting at the character specified by offset.
...
If offset evaluates to a number less than zero, the value is used as an offset in characters from the end of the value of parameter. ... Note that a negative offset must be separated from the colon by at least one space to avoid being confused with the ‘:-' expansion.

${parameter:offset}
${parameter:offset:length}

这称为子串扩展。它扩展到从 offset 指定的字符开始的参数值的长度字符。
...
如果 offset 计算结果为小于零的数字,则该值用作距参数值末尾的字符偏移量。...请注意,负偏移量必须与冒号隔开至少一个空格,以避免与 ':-' 扩展混淆。

回答by chepner

In this case, the colon is just a modifier for the -operator. ${branch-HEAD}would expand to "HEAD" only if branchis unset, while ${branch:-HEAD}expands to "HEAD" if branchis the null string as well.

在这种情况下,冒号只是-运算符的修饰符。${branch-HEAD}仅当branch未设置时才${branch:-HEAD}扩展为“HEAD”,如果branch也是空字符串,则扩展为“HEAD” 。

$ branch=master
$ echo "${branch-HEAD} + ${branch:-HEAD}"
master + master
$ branch=""
$ echo "${branch-HEAD} + ${branch:-HEAD}"
 + HEAD
$ unset branch
$ echo "${branch-HEAD} + ${branch:-HEAD}"
HEAD + HEAD

回答by ruizpauker

In bash, ${VAR1:-VAR2}is equivalent to SQL's coalesce(VAR1, VAR2), or C#'s VAR1 ?? VAR2.

在bash,${VAR1:-VAR2}相当于SQL的coalesce(VAR1, VAR2),或者C#的VAR1 ?? VAR2

In your case:

在你的情况下:

branch_name=`git describe --contains --all HEAD`
branch_name=${branch_name:-HEAD}

The first line executes the gitcommand and sets the value in the branch_namevariable, then, the second line coalesces its value assigning the value of HEADif branch_nameis null.

第一行执行git命令并设置branch_name变量中的值,然后,第二行合并其值并分配HEADif 的branch_name值为 null。

As you said ${VAR1:NUM}is a string prefix operation (leftin SQL), which when used with a negative number, as ${VAR1: -NUMBER}becomes a suffix (right) operation. Note the whitespace before the minus sign: if you skip that whitespace it becomes the coalesceoperation as I've said before.

正如您所说的${VAR1:NUM}是字符串前缀操作(left在 SQL 中),当与负数一起使用时, as${VAR1: -NUMBER}成为后缀 ( right) 操作。注意减号前的空格:如果你跳过那个空格,它就会变成coalesce我之前说过的操作。