== Faking (or not) a ternary if operator with _&&_ and _||_ Suppose that you have a multiline if in some language and you want to rewrite this into a single line and a single expression; however, your language only has _&&_ and _||_ operators without a ternary if operator (this is C's _?:_ operator). Can you do this rewrite safely? To be concrete, let's do this in the Bourne shell (which is where I saw this be done recently). Suppose you have the following multiline bit of shell script and you want to reduce it to something that's as short as possible: > if cmd1; then > cmd2 > else > cmd3 > fi Is a good translation of this '_cmd1 && cmd2 || cmd3_'? (Let's ignore [[the set -e gotcha BourneSetEGotcha]].) The answer is no. It's easy to see why this is if I rewrite this with specific commands: > _true && false || echo oops_ This will echo 'oops'. The answer to the question turns out to depend on whether you care about the value of the ternary if (or the multiline if). If you don't care about the value and are evaluating things purely for their side effects, then you can write '_a ? b : c_' as '_a && (b || true) || c_', which avoids the problem by forcing the middle clause to always be considered true. If you do need to expose the (truth) value of _b_, this rewrite is no good and you are almost certainly up the creek; the only way out is if you know that _b_'s value will always considered true. (I first saw this problem [[in Python ../python/MoreAndOrAbuse]], but seeing it reappear in the Bourne shell started me down the path of thinking about the general issue.) The Bourne shell version of this rewrite would be > _cmd1 && (cmd2 || true) || cmd3_ I think that most of the time this should be fine; it would be very odd style to check the exit code of _cmd2_ or _cmd3_ after the _if_, and certainly [[the example I saw this in http://serverfault.com/questions/377921/whats-the-advantage-of-using-a-bash-script-for-cron-jobs/377958#377958]] didn't do it. My one twitch is that I am reflexively nervous about _()_ potentially introducing surprise subshells, but if _cmd2_ is a real command that's irrelevant.