Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

BASH: Does it support conditional variables like var="test"?"1":"2"

I am new to bash but have done lots of PHP, and Javascript.

Is there some sort of equivilent to this PHP?

$default = 10;
$var = (!$var) ? $default : $var;

Thanks

like image 493
nicholas.alipaz Avatar asked Sep 25 '09 04:09

nicholas.alipaz


1 Answers

Yes, it does:

var=${var:-10}

Even with other variables:

unset var
export def=99
echo ${var:-${def}} # gives '99'
export var=7
echo ${var:-${def}} # gives '7'
like image 120
paxdiablo Avatar answered Oct 12 '22 09:10

paxdiablo