什么是$之间的区别{VAR:-word}和$ {VAR词}? [英] What is the difference between ${var:-word} and ${var-word}?

查看:512
本文介绍了什么是$之间的区别{VAR:-word}和$ {VAR词}?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我发现了一个bash脚本下面的命令:

I found the following command in a bash script:

git blame $NOT_WHITESPACE --line-porcelain "${2-@}" -- "$file"

这是什么 $ {2 - @} 是什么意思?尝试,它返回第二个参数,@,如果它不存在。 根据文档 $ {2: - @} 也应该这样做。我试了一下,确实是不相同。有什么不同?它被记录在哪里?手册页似乎并没有说这个符号什么。

What does this ${2-@} mean? Trying out, it returns the 2nd argument, and "@" if it doesn't exist. According to the documentation, ${2:-@} should do the same. I tried it, and it indeed does the same. What's the difference? Where is it documented? The man page does not seem to say anything about this notation.

推荐答案

黑客维基 - 参数扩展

$ {参数:-word}

${PARAMETER:-WORD}

$ {参数WORD}

${PARAMETER-WORD}

如果参数参数是未设置(从来没有被定义),或者为null
  (空),这一个扩展到字,否则它扩展为值
  参数,仿佛它只是$ {}参数。 如果你忽略了:
  (结肠),如示出在第二个形式中,该缺省值只用于
  当参数被清除的,而不是当它是空的。

If the parameter PARAMETER is unset (never was defined) or null (empty), this one expands to WORD, otherwise it expands to the value of PARAMETER, as if it just was ${PARAMETER}. If you omit the : (colon), like shown in the second form, the default value is only used when the parameter was unset, not when it was empty.

echo "Your home directory is: ${HOME:-/home/$USER}."
echo "${HOME:-/home/$USER} will be used to store your personal data."

如果HOME没有设置或者为空,每次要打印的东西
  有用的,你需要把该参数语法

If HOME is unset or empty, everytime you want to print something useful, you need to put that parameter syntax in.

#!/bin/bash

read -p "Enter your gender (just press ENTER to not tell us): " GENDER
echo "Your gender is ${GENDER:-a secret}."


  
  

这将打印你的性别是一个秘密。当你不进入
  性别。请注意,在扩张时则使用默认值,它是
  不分配给参数

It will print "Your gender is a secret." when you don't enter the gender. Note that the default value is used on expansion time, it is not assigned to the parameter.

这篇关于什么是$之间的区别{VAR:-word}和$ {VAR词}?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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