There seems to be some misunderstanding here about the bashBash builtin true
, and more specifically, about how bashBash expands and interprets expressions inside brackets.
The code in miku's answermiku's answer has absolutely nothing to do with the bashBash builtin true
, nor /bin/true
, nor any other flavor of the true
command. In this case, true
is nothing more than a simple character string, and no call to the true
command/builtin is ever made, neither by the variable assignment, nor by the evaluation of the conditional expression.
The only difference here is that the four characters being compared are 'y', 'e', 'a', and 'h' instead of 't', 'r', 'u', and 'e'. That's it. There's no attempt made to call a command or builtin named yeah
, nor is there (in miku's example) any sort of special handling going on when bashBash parses the token true
. It's just a string, and a completely arbitrary one at that.
Update (2/19/2014-02-19): After following the link in miku's answer, now I see where some of the confusion is coming from. Miku's answer uses single brackets, but the code snippet he links to does not use brackets. It's just:
Here's what bashBash is doing in each case:
- Expand the variable
$the_world_is_flat
to the string"true"
. - Attempt to parse the string
"true"
as a command. - Find and run the
true
command (either a builtin or/bin/true
, depending on bashthe Bash version). - Compare the exit code of the
true
command (which is always 0) with 0. Recall that in most shells, an exit code of 0 indicates success and anything else indicates failure. - Since the exit code was 0 (success), execute the
if
statement'sthen
clause
The no-brackets code works, because the true
command returns an exit code of 0, which indicates success. The bracketed code works, because the value of $the_world_is_flat
is identical to the string literal true
on the right side of the =
.
This code (if run with root privsprivileges) will reboot your computer:
Update (4/14/2014-04-14) To answer the question in the comments regarding the difference between =
and ==
: AFAIK, there is no difference. The ==
operator is a bashBash-specific synonym for =
, and as far as I've seen, they work exactly the same in all contexts.
Note, however, that I'm specifically talking about the =
and ==
string comparison operators used in either [ ]
or [[ ]]
tests. I'm not suggesting that =
and ==
are interchangeable everywhere in bash.
For example, you obviously can't do variable assignment with ==
, such as var=="foo"
(well technically you can do this, but the value of var
will be "=foo"
, because bashBash isn't seeing an ==
operator here, it's seeing an =
(assignment) operator, followed by the literal value ="foo"
, which just becomes "=foo"
).
Also, although =
and ==
are interchangeable, you should keep in mind that how those tests work does depend on whether you're using it inside [ ]
or [[ ]]
, and also on whether or not the operands are quoted. YouYou can read more about that here:in Advanced Bash Scripting Guide: 7.3 Other Comparison OperatorsAdvanced Bash Scripting Guide: 7.3 Other Comparison Operators (scroll down to the discussion of =
and ==
).