80

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
codeforester
  • 39,467
  • 16
  • 112
  • 140
Stelios
  • 1,294
  • 3
  • 12
  • 28

1 Answers1

112

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

ThomasMcLeod
  • 7,603
  • 4
  • 42
  • 80
Gilles Quénot
  • 173,512
  • 41
  • 224
  • 223
  • 2
    Rather than "void", it's more accurate to say "unset or null". – chepner Dec 12 '14 at 14:59
  • The link is dead (possibly temporarily) - there is a mirror here: https://github.com/rawiriblundell/wiki.bash-hackers.org/blob/main/syntax/pe.md#use-a-default-value – Colin Pickard Jul 19 '23 at 11:57