Reputation: 19004
What does the following bash snippet do exactly? ${2:-${1}}
Upvotes: 26
Views: 16989
Reputation: 12976
It means "Use the second argument if the first is undefined or empty, 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)".
Upvotes: 3
Reputation: 361595
${var:-default}
evaluates to the value of $var
, unless $var
isn'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 $2
if there were two arguments passed, otherwise return $1
.
Upvotes: 19
Reputation: 19004
It gives the value of ${2} if defined or defaults to ${1} http://jaduks.livejournal.com/7934.html
Upvotes: 1
Reputation: 881595
"Use the second argument, but if none then the first one".
Upvotes: 30