${2:-${1}} 在 Bash 中是什么意思?

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

What does ${2:-${1}} mean in Bash?

bash

提问by Ben McCann

What does the following bash snippet do exactly? ${2:-${1}}

以下 bash 代码段究竟做了什么?${2:-${1}}

回答by Alex Martelli

"Use the second argument, but if none then the first one".

“使用第二个参数,但如果没有,则使用第一个”。

回答by John Kugelman

${var:-default}evaluates to the value of $var, unless $varisn't set in which case it evaluates to the text "default". $1, $2, et al are the command-line arguments to your program (or function). Putting the two together it means to return $2if there were two arguments passed, otherwise return $1.

${var:-default}计算为 的值$var,除非$var未设置在这种情况下它计算为 text "default"$1, $2, et al 是程序(或函数)的命令行参数。将两者放在一起意味着$2如果传递了两个参数则返回,否则返回$1

回答by Kevin Little

It means "Use the second argument if the first is undefined orempty, else use the first". The form "${2-${1}}" (no ':') means "Use the second if the first is not defined (but if the first is defined as empty, use it)".

这意味着“如果第一个参数未定义或为空,则使用第二个参数,否则使用第一个参数”。形式“${2-${1}}”(无':')表示“如果第一个未定义,则使用第二个(但如果第一个定义为空,则使用它)”。

回答by Ben McCann

It gives the value of ${2} if defined or defaults to ${1} http://jaduks.livejournal.com/7934.html

如果定义或默认为 ${1},则它给出 ${2} 的值 http://jaduks.livejournal.com/7934.html