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.

9
  • The no-bracket approach also has the advantage of letting you write clean, clear (imo) one-liners like $the_world_is_flat && echo "you are in flatland!"
    – ajk
    Commented Feb 20, 2014 at 1:11
  • 10
    True. Although, I'm not advocating for (or against) either approach. I just wanted to clear up some of the misinformation that's getting voted up here, so that people who stumble upon this topic later on won't walk away with a bunch of misconceptions about how this all works.
    – Mike Holt
    Commented Feb 20, 2014 at 1:19
  • 1
    The reason for the confusion is that miku's original answer stood for 4 years. All the references to the builtin true were made regarding the original answer. (The revised answer on Feb 12, 2014 was not submitted by miku.) I have edited the answer to include both original and revised. Then people's comments make sense.
    – wisbucky
    Commented Jun 8, 2015 at 23:10
  • 1
    From reading the answers offered here, I get the impression that there's no such thing as actually using the real true. Is there a way? I suspect many programmers who are used to stricter languages viewing this answer to assist them in mixing up some bash glue to make their lives a bit easier would want an === operator so that strings and "booleans" aren't actually interchangeable. Should they just stick to 0 and 1 and use (( $maybeIAmTrue )) as suggested in Quolonel Question's answer? Commented Jul 23, 2015 at 23:59
  • 3
    To address SeldomNeedy's comment, yes, you can use the real true, but generally not as something to compare a variable against, since the real true has no value per se. All it does is set the exit status to 0, indicating success. It's worth noting that it's essentially equivalent to the so-called "null command", or :. As far as using 0 and 1, that's what I do in all my scripts these days where I need booleans. And I use the (( )) operator instead of [[ ]] to evaluate. So, for example, if I have flag=0, I can then do if (( flag )); then ...
    – Mike Holt
    Commented Jul 24, 2017 at 19:58