Tests and Comparisons: Alternatives

Advanced Bash-Scripting Guide: An in-depth exploration of the art of shell scripting
Prev | Chapter 36. Miscellany | Next

# 36.3. Tests and Comparisons: Alternatives

For tests, the [[ ]] construct may be more appropriate than `[ ]`. Likewise, arithmetic comparisons might benefit from the (( )) construct.

       1 a=8
       2
       3 # All of the comparisons below are equivalent.
       4 test "$a" -lt 16 && echo "yes, $a < 16"         # "and list"
       5 /bin/test "$a" -lt 16 && echo "yes, $a < 16"
       6 [ "$a" -lt 16 ] && echo "yes, $a < 16"
       7 [[ $a -lt 16 ]] && echo "yes, $a < 16"          # Quoting variables within
       8 (( a < 16 )) && echo "yes, $a < 16"             # [[ ]] and (( )) not necessary.
       9
      10 city="New York"
      11 # Again, all of the comparisons below are equivalent.
      12 test "$city" \< Paris && echo "Yes, Paris is greater than $city"
      13                                   # Greater ASCII order.
      14 /bin/test "$city" \< Paris && echo "Yes, Paris is greater than $city"
      15 [ "$city" \< Paris ] && echo "Yes, Paris is greater than $city"
      16 [[ $city < Paris ]] && echo "Yes, Paris is greater than $city"
      17                                   # Need not quote $city.
      18
      19 # Thank you, S.C.

Prev | Home | Next
Shell Wrappers | Up | Recursion: a script calling itself