Ben McCann
Ben McCann

Reputation: 19004

What does ${2:-${1}} mean in Bash?

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

Upvotes: 26

Views: 16989

Answers (4)

Kevin Little
Kevin Little

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

John Kugelman
John Kugelman

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

Ben McCann
Ben McCann

Reputation: 19004

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

Upvotes: 1

Alex Martelli
Alex Martelli

Reputation: 881595

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

Upvotes: 30

Related Questions