O'Reilly logo

Mastering Bash by Giorgio Zarrelli

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

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 ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required