Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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 
like image 493
Stelios Avatar asked Dec 12 '14 14:12

Stelios


People also ask

What is double colon in Bash?

It's nothing, these colons are part of the command names apparently. You can verify yourself by creating and running a command with : in the name. The shell by default will autoescape them and its all perfectly legal. Follow this answer to receive notifications.

What Does a colon mean in Bash?

Bash and sh both use colons (":") in more than one context. You'll see it used as a separator ($PATH, for example), as a modifier (${n:="foo"}) and as a null operator ("while :").

What does colon mean in command line?

It is used when a command is needed, as in the then condition of an if command, but nothing is to be done by the command.

What is Dash D in Bash?

-d is a operator to test if the given directory exists or not. For example, I am having a only directory called /home/sureshkumar/test/. The directory variable contains the "/home/sureshkumar/test/" if [ -d $directory ]


1 Answers

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

like image 75
Gilles Quenot Avatar answered Sep 25 '22 08:09

Gilles Quenot