Skip to main content

You are not logged in. Your edit will be placed in a queue until it is peer reviewed.

We welcome edits that make the post easier to understand and more valuable for readers. Because community members review edits, please try to make the post substantially better than how you found it, for example, by fixing grammar or adding additional resources and hyperlinks.

8
  • 92
    this is better than the others... the point about the tertiary operator is that it's an operator, hence it's proper context is in an expression, hence it must return a value. Commented Mar 16, 2012 at 7:56
  • 3
    This is the most concise way. Be aware that if the part with echo "$c" is an aliased, multi-lined command (like echo 1; echo 2), you should enclose it in parentheses. Commented Sep 30, 2013 at 18:04
  • 2
    This will also capture any output of the tested command, too (yes, in this particular case, we "know" it doesn't produce any). Commented Jan 23, 2018 at 20:16
  • 30
    This chain of operators only behaves like a ternary operator if you are positive that the command after && won't have a non-zero exit status. Otherwise, a && b || c will "unexpectedly" run c if a succeeds but b fails. Commented May 15, 2018 at 17:57
  • 5
    several issues with this, starts a subshell, uses echo and doesn't quote the result. To be honest this approach is shoehorning the concept of a ternary operator into a language that doesn't have one. If I came across this in the wild I would be bewildered Commented Jan 24, 2023 at 10:36