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.

20
  • 50
    To explain what is happening: the if statement is executing the contents of the variable which is the Bash builtin true. Any command could be set as the value of the variable and its exit value would be evaluated. Commented Jun 2, 2010 at 4:16
  • 8
    @pms The operators "-o" and "-a" are only for the "test" command (aka "[]"). Instead, this is "if + command", without the "test". (Like "if grep foo file; then ...".) So, use the normal && and || operators: # t1=true; t2=true; f1=false; # if $t1 || $f1; then echo is_true ; else echo is_false; fi; (returns "true", since t1=true) # if $t1 && $f1 || $t2; then echo is_true ; else echo is_false; fi (returns "true", since t2=true) . Again, this ONLY works because "true"/"false" are bash-builtins (returning true/false). You can't use "if $var..." unless var is a cmd (ie, true or false)
    – michael
    Commented Jun 30, 2012 at 9:07
  • 22
    -1, see my answer for an explanation.
    – Dennis
    Commented Jan 18, 2014 at 23:04
  • 4
    Lots of incorrect information, here. /bin/true isn't being used effectively. See Dennis' answer.
    – ajk
    Commented Feb 20, 2014 at 1:09
  • 2
    About the newer answer: it is simple string comparison. When in bash, you should rather use double square brackets and quote everything so people know it's strings and nothing special.
    – Hugo G
    Commented Nov 3, 2017 at 9:12