Testing integers

As we saw with comparisons between files, we can do much the same thing with integers using some binary operators. This comes handy in case we want to take a decision based on the value a variable has taken, as we saw in earlier examples, and it is a type of action widely performed when dealing with scripts:

  • -eq: This is true if the first integer is equal to the second:
#!/bin/bash    echo "Hello user, please type in one integer and press enter:"read user_input1 echo "Now type in the number again and press enter:"read user_input2 if [ ${user_input1} -eq ${user_input2} ]thenecho "Great! The integer ${user_input1} is equal to ${user_input2}"elseecho "The integer ${user_input1} is not equal to ${user_input2}..."fi  

The code is ...

Get Mastering Bash 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.