Evaluating binary numbers

Let's say we want to introduce another if condition and use elif (short for else if):

#!/bin/bashAGE=21if [ ${AGE} -lt 18 ]; then echo "You must be 18 or older to see this movie"elif [ ${AGE} -eq 21 ]; then echo "You may see the movie and get popcorn"else echo "You may see the movie!" exit 1fiecho "This line might not get executed"

If AGE is set and equals 21, then the snippet will echo:

You may see the movie and get popcornThis line might not get executed

Using if, elif, and else, combined with other evaluations, we can execute specific branches of logic and functions or even exit our script. To evaluate raw binary variables, use the following operators:

  • -gt (greater than >)
  • -ge (greater or equal to >=)
  • -lt (less ...

Get Bash Cookbook now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.