Skip to main content
Added a cross reference. Active reading [<http://en.wikipedia.org/wiki/Bash_%28Unix_shell%29>]. Changed to ISO 8601 date.
Source Link
Peter Mortensen
  • 31.4k
  • 22
  • 109
  • 132

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:

  1. Expand the variable $the_world_is_flat to the string "true".
  2. Attempt to parse the string "true" as a command.
  3. Find and run the true command (either a builtin or /bin/true, depending on bashthe Bash version).
  4. 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.
  5. Since the exit code was 0 (success), execute the if statement's then 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 ==).

There seems to be some misunderstanding here about the bash builtin true, and more specifically, about how bash expands and interprets expressions inside brackets.

The code in miku's answer has absolutely nothing to do with the bash 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 bash parses the token true. It's just a string, and a completely arbitrary one at that.

Update (2/19/2014): 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 bash is doing in each case:

  1. Expand the variable $the_world_is_flat to the string "true".
  2. Attempt to parse the string "true" as a command.
  3. Find and run the true command (either a builtin or /bin/true, depending on bash version).
  4. 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.
  5. Since the exit code was 0 (success), execute the if statement's then 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 privs) will reboot your computer:

Update (4/14/2014) To answer the question in the comments regarding the difference between = and ==: AFAIK, there is no difference. The == operator is a bash-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 bash 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. You can read more about that here: Advanced Bash Scripting Guide: 7.3 Other Comparison Operators (scroll down to the discussion of = and ==).

There seems to be some misunderstanding here about the Bash builtin true, and more specifically, about how Bash expands and interprets expressions inside brackets.

The code in miku's answer has absolutely nothing to do with the Bash 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 Bash parses the token true. It's just a string, and a completely arbitrary one at that.

Update (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 Bash is doing in each case:

  1. Expand the variable $the_world_is_flat to the string "true".
  2. Attempt to parse the string "true" as a command.
  3. Find and run the true command (either a builtin or /bin/true, depending on the Bash version).
  4. 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.
  5. Since the exit code was 0 (success), execute the if statement's then 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 privileges) will reboot your computer:

Update (2014-04-14) To answer the question in the comments regarding the difference between = and ==: AFAIK, there is no difference. The == operator is a Bash-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 Bash 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. You can read more about that in Advanced Bash Scripting Guide: 7.3 Other Comparison Operators (scroll down to the discussion of = and ==).

deleted 1 character in body
Source Link
Mike Holt
  • 4.6k
  • 1
  • 18
  • 24
the_world_is_flat=yeah
if [ "$the_world_is_flat" === yeah ]; then
    echo 'Be careful not to fall off!'
fi
the_world_is_flat=yeah
if [ "$the_world_is_flat" == yeah ]; then
    echo 'Be careful not to fall off!'
fi
the_world_is_flat=yeah
if [ "$the_world_is_flat" = yeah ]; then
    echo 'Be careful not to fall off!'
fi
added 993 characters in body
Source Link
Mike Holt
  • 4.6k
  • 1
  • 18
  • 24

Update (4/14/2014) To answer the question in the comments regarding the difference between = and ==: AFAIK, there is no difference. The == operator is a bash-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 bash isn't seeing an == operator here, it's seeing an = (assignment) operator, followed by the literal value ="foo", which just becomes "=foo").

Update (4/14/2014) To answer the question in the comments regarding the difference between = and ==: AFAIK, there is no difference. The == operator is a bash-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".

Update (4/14/2014) To answer the question in the comments regarding the difference between = and ==: AFAIK, there is no difference. The == operator is a bash-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 bash isn't seeing an == operator here, it's seeing an = (assignment) operator, followed by the literal value ="foo", which just becomes "=foo").

added 993 characters in body
Source Link
Mike Holt
  • 4.6k
  • 1
  • 18
  • 24
Loading
deleted 1 characters in body
Source Link
Mike Holt
  • 4.6k
  • 1
  • 18
  • 24
Loading
deleted 1 characters in body
Source Link
Mike Holt
  • 4.6k
  • 1
  • 18
  • 24
Loading
added 2153 characters in body
Source Link
Mike Holt
  • 4.6k
  • 1
  • 18
  • 24
Loading
Source Link
Mike Holt
  • 4.6k
  • 1
  • 18
  • 24
Loading