The difference between ‘&&’ and ‘-a’

NeilZhang
NeilZhang
管理员
140
文章
106.8千
浏览
Linux评论213字数 562阅读1分52秒阅读模式

These days I am reading the book 'Advanced Bash-Scripting Guide' and I got confused about '&&' and '-a'.

The related part was here:

  1. [ 1 -eq 1 ] && [ -n "`echo true 1>&2`" ] # true
  2. [ 1 -eq 2 ] && [ -n "`echo true 1>&2`" ] # (no output)
  3. # ^^^^^^^ False condition. So far, everything as expected.
  4. # However ...
  5. [ 1 -eq 2 -a -n "`echo true 1>&2`" ] # true
  6. # ^^^^^^^ False condition. So, why "true" output?
  7. # Is it because both condition clauses within brackets evaluate?
  8. [[ 1 -eq 2 && -n "`echo true 1>&2`" ]] # (no output)
  9. # No, that's not it.
  10. # Apparently && and || "short-circuit" while -a and -o do not.

The last line explained the reason, but I wanted to find more evidences from the BASH manual and I got it.

  1. For && and ||:
  2. Expressions may be combined using the following operators, listed in decreasing order of precedence:
  3.  
  4. ( expression )
  5. Returns the value of expression. This may be used to override the normal precedence of operators.
  6. ! expression
  7. True if expression is false.
  8. expression1 && expression2
  9. True if both expression1 and expression2 are true.
  10. expression1 || expression2
  11. True if either expression1 or expression2 is true.
  12.  
  13. The && and || operators do not evaluate expression2 if the value of expression1 is sufficient to determine the return value of the entire condi-
  14. tional expression.
  15.  
  16. For -a and -o:
  17. Expressions may be combined using the following operators, listed in decreasing order of precedence. The evaluation depends on the number of
  18. arguments; see below. Operator precedence is used when there are five or more arguments.
  19. ! expr True if expr is false.
  20. ( expr )
  21. Returns the value of expr. This may be used to override the normal precedence of operators.
  22. expr1 -a expr2
  23. True if both expr1 and expr2 are true.
  24. expr1 -o expr2
  25. True if either expr1 or expr2 is true.
  26.  
  27. test and [ evaluate conditional expressions using a set of rules based on the number of arguments.
  28.  
  29. 0 arguments
  30. The expression is false.
  31. 1 argument
  32. The expression is true if and only if the argument is not null.
  33. 2 arguments
  34. If the first argument is !, the expression is true if and only if the second argument is null. If the first argument is one of the unary
  35. conditional operators listed above under CONDITIONAL EXPRESSIONS, the expression is true if the unary test is true. If the first argument
  36. is not a valid unary conditional operator, the expression is false.
  37. 3 arguments
  38. The following conditions are applied in the order listed. If the second argument is one of the binary conditional operators listed above
  39. under CONDITIONAL EXPRESSIONS, the result of the expression is the result of the binary test using the first and third arguments as oper
  40. ands. The -a and -o operators are considered binary operators when there are three arguments. If the first argument is !, the value is
  41. the negation of the two-argument test using the second and third arguments. If the first argument is exactly ( and the third argument is
  42. exactly ), the result is the one-argument test of the second argument. Otherwise, the expression is false.
  43. 4 arguments
  44. If the first argument is !, the result is the negation of the three-argument expression composed of the remaining arguments. Otherwise,
  45. the expression is parsed and evaluated according to precedence using the rules listed above.
  46. 5 or more arguments
  47. The expression is parsed and evaluated according to precedence using the rules listed above.

So for '-a' and '-o', both sides will ALWAYS be executed.

 
  • 本文由 NeilZhang 发表于18/08/2018 17:49:29
  • Repost please keep this link: https://www.dbcloudsvc.com/blogs/linux/the-difference-between-and-a/
匿名

发表评论

匿名网友
:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:
确定