What does the following bash snippet do exactly? ${2:-${1}}
A:
It gives the value of ${2} if defined or defaults to ${1} http://jaduks.livejournal.com/7934.html
Ben McCann
2009-07-22 05:07:01
+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
2009-07-22 05:09:41
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
2009-07-28 22:50:29