在变量替换Bash的冒号运算符? [英] Bash colon operator in variable substitution?

查看:245
本文介绍了在变量替换Bash的冒号运算符?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我继承了一些的bash code和这两条线扑朔迷离我:

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

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

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

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

推荐答案

这需要变量 branch_name ,如果它被定义。如果没有定义,使用 HEAD 代替。

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

请参阅壳牌参数扩展了解详情。

子串如下涵盖了几行。两者之间的区别是

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

${parameter:-word}

VS

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

这篇关于在变量替换Bash的冒号运算符?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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