bash 中的冒号破折号“:-”是什么意思

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

What does the colon dash ":-" mean in bash

bashshellsyntaxsemantics

提问by Stelios

The result is the one desired; after a bit of trial and error. I don't understand what the "2:-" and "3:-" do/mean. Can someone explain.

结果是想要的;经过一些试验和错误。我不明白“2:-”和“3:-”是什么意思。有人可以解释一下。

#!/bin/bash
pid=$(ps -ef | grep java | awk ' NR ==1 {print }')

count=${2:-30}  # defaults to 30 times
delay=${3:-10} # defaults to 10 second
mkdir $(date +"%y%m%d")
folder=$(date +"%y%m%d")
while [ $count -gt 0 ]
do
    jstack $pid >./"$folder"/jstack.$(date +%H%M%S.%N)
    sleep $delay
    let count--
    echo -n "."
done

回答by Gilles Quenot

It's a parameter expansion, it means if the third argument is null or unset, replace it with what's after:-

这是一个参数扩展,这意味着如果第三个参数为空或未设置,则将其替换为后面的内容:-

$ x=
$ echo ${x:-1}
1
$ echo $x

$

There's also another similar PE that assign the value if the variable is null:

如果变量为空,还有另一个类似的 PE 分配值:

$ x=
$ echo ${x:=1}
1
$ echo $x
1

Check http://wiki.bash-hackers.org/syntax/pe

检查http://wiki.bash-hackers.org/syntax/pe