Testing integers

As well as testing string values of bash scripts, we can test for integer values and whole numbers. Another way of testing the input of a script is to count the numbers of positional parameters and also test if the number is above 0:

test $# -gt 0 

Or using the brackets, as follows:

[ $# -gt 0 ] 

When in a relationship, the top positional parameters of the $# variable represent the number of parameters passed to the script.

There are many tests that can be done for numbers:

  • number1 -eq number2: This checks if number1 is equal to number2
  • number1 -ge number2: This checks if number1 is greater than or equal to number2.
  • number1 -gt number2: This checks if number1 is greater than number2
  • number1 -le number2: This checks if ...

Get Mastering Linux Shell Scripting 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.