Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Bash "inline" conditionals

My goal is to add an argument to a program being executed if a bash variable is existent, as so:

bob -a some_arg (( if we have ${VAR} defined add '-b ${VAR}' as an argument ))

I'd like to avoid something like:

if [[ -z ${VAR} ]]; then
    bob -a some_arg
else
    bob -a some_arg -b ${VAR}
fi

Although, it is the only option?

like image 678
Matoe Avatar asked Sep 30 '12 15:09

Matoe


1 Answers

Using bash parameter expansion :

bob -a some_arg ${VAR:+-b "$VAR"}

Some good doc : http://wiki.bash-hackers.org/syntax/pe

And also LANG=C man bash | less +/'Parameter Expansion'

like image 56
Gilles Quenot Avatar answered Oct 11 '22 11:10

Gilles Quenot