Ryan Schubert
Ryan Schubert

Reputation: 186

when is double parenthesis construct for conditional statements useful?

I've been reading the Advanced Bash Scripting Guide trying to learn more about what is considered best practice and came across this way of constructing conditionals:

#condition?result-if-true:result-if-false

(( var0 = var1<98?9:21 ))
#                ^ ^

# if [ "$var1" -lt 98 ]
# then
#   var0=9
# else
#   var0=21
# fi

It looks cool but is there an advantage to this outside of how compact it is?

Upvotes: 1

Views: 87

Answers (1)

Calvin Kim
Calvin Kim

Reputation: 364

It may look cool in geeky eyes, but it's difficult to read. There is no advantage to this. So stick to if-then-else or put a lot of comments on it just like you did in your question.

Upvotes: 2

Related Questions