tags:

views:

148

answers:

4

What does the following bash snippet do exactly? ${2:-${1}}

+3  A: 

"Use the second argument, but if none then the first one".

Alex Martelli
A: 

It gives the value of ${2} if defined or defaults to ${1} http://jaduks.livejournal.com/7934.html

Ben McCann
+2  A: 

${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.

John Kugelman
A: 

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)".

Kevin Little