What does the colon dash “:-” mean in bash [duplicate]

懵懂的女人 提交于 2019-11-26 22:56:15

问题


This question already has an answer here:

  • Usage of :- (colon dash) in bash 2 answers

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.

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

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

回答1:


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:

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

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



来源:https://stackoverflow.com/questions/27445455/what-does-the-colon-dash-mean-in-bash

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!