September 18, 2012
Bash Conditional Operator
Working on the new.sh
and new_c.sh
projects today, and I though it was about time to resolve a vexing issue: can bash
execute conditional operations like in several other programming languages like C, C++, Java, PHP, etc.?
The answer is a resounding "YES!"
Firstly, what is the conditional operator? The conditional operator is the ternary operator that many programming languages have to execute an "if-then-else" (or "if-else" if you prefer to say it like that), in a single line. The following figure demonstrates its usefulness.
Without ?: | With ?: |
---|---|
if ( a == b ) { return x; } else { return y; } |
return ( a == b ) ? x : y; |
Sure, you can argue that you could write the code on the left as
if (a == b ) { return x; } else { return y; }
But look how much better the code on the right looks. Add to that, only one return
is used.
Commonly, the conditional operator is use for conditional assignment, as demonstrated in the next figure which looks quite similar to the previous figure.
Without ?: | With ?: |
---|---|
if ( a == b ) { c = x; } else { c = y; } |
c = ( a == b ) ? x : y; |
In some languages, assuming they got their order of operations right, you could even get away with this line of code.
c += ( a == b ) ? x : y; // c = c + (x or y)
I know this isn't a big deal if C, C++, Java, JavaScript, or PHP are you forte, but but what about Bash? Bash is so literal at times.
Let's take the following code example. This next figure demonstrates a Bash, if-then-else statement, assuming we don't add any short cuts to the syntax.
Most of the time you can add a semicolon between the right test
bracket. You can also do this with for
, while
and until
loops with the do
keyword.
So we made no significant change to this line of code. But we're only doing one thing. Let's flatten this to one line, like if we were trying something out in the command line.
So we've managed to whittle this down to one line, but those keywords are still there. Here's where the magic happens.
"But this doesn't look like the condition operation syntax", I hear you cry. Well, let's make it look like it.
Now, I see there many be a few folks dissatisfied that a couple of echo
commands got in there. There is of course this alternative.
or even
But honestly, I'm content with either of the following two forms.
At any rate, I'm considering practicing this syntax in the future, maybe even in the latest version of the new
and new_c
programs.