A number is a combination of 0-9 digits—the Bash variables stores all value in the form of strings. Even if the stored value is in string format, we can perform all the arithmetical operations if the stored value is a valid number. As a best practice, we should verify the values of variables before performing the arithmetic operations.

A number can be an integer number, a floating point number, or a positive/negative number that prefixes with a “ and -” symbol. In this tutorial, we have discussed a few methods to verify that the variable contains only digits, integers, and double or float values, not alphabets.

Using Equal Tilde (=~) Operator

The regular expression is a quick and easy way for checking if a value is a number. In Bash scripting, the equal tilde (=~) operator is used to compare a value with a regular expression. That can be used with the bash if statement:

#!/usr/bin/env bash

# Shell script to check if the input number is an integer

read p “Please enter a number: “ VAR

if [[ $VAR =~ ^[09] $ ]]; then

   echo “${VAR} is a number”

else

   echo “${VAR} is not a number”

fi

Write the above snippet in a shell script and execute. first-time input a value number.

First run

Please enter a number: 12 12 is a number

Again run the script with some alphabets and check the output:

Second run

Please enter a number: 1a2b 12a is not a number

Check if Floating Point Number

A floating Point Number is an integer type that represents a number that can hold a fractional part (also known as a float). As the name suggests, these numbers can take on different values at different places and times. Floating point numbers are typically used to represent numbers with a decimal point in them. For example, 1.0, 0.6, and 10.26 are all floating-point numbers.

Write a new shell script that considers a floating point number as a valid number.

#!/usr/bin/env bash

# Shell script to check if the input number is a number

# That can be a integer or floating point number

read p “Please enter a number: “ VAR

if [[ $VAR =~ ^[09] ([.][09] )?$ ]]; then

   echo “${VAR} is a number”

else

   echo “${VAR} is not a number”

fi

Execute the above script by inputting some floating point numbers. This script will consider all floating point numbers as valid numbers.

First run

Please enter a number: 12.10 12.10 is a number

Using Switch Case Statment

Some of the scripts required to have a case statement. That is similar to switch statements in other programming languages. We can also use regular expressions in case statement options and verify if the given input is a number.

#!/usr/bin/env bash

# Shell script to check if the input number is a number

# using switch case statement

read p “Please enter a number: “ VAR

case $VARin

    |*[!09]*) echo “${VAR} is not a number” ;;

    *) echo “${VAR} is a number” ;;

esac

Execute the above script multiple times with different inputs and see the results:

First run

Please enter a number: 12 12 is a number

Check if Number Contains /- Signs

In the above methods, we have checked for integer values that contain 0-9 digits only, A floating point number that also contains a fraction of values known as decimal numbers. In some cases, a number also can be positive or negative. Any number with no prefix or with is a positive number. The number with - prefix is negative number.

Update the regular expression, to consider a number that has a /- sings before it.

#!/usr/bin/env bash

# Shell script to check if the input number is a number

# and can be a positive or negative number

read p “Please enter a number: “ VAR

if [[ $VAR =~ ^[]?[09] $ ]]; then

   echo “${VAR} is a number”

else

   echo “${VAR} is not a number”

fi

Run the above script with different-2 inputs and check the results.

First run

Please enter a number: 12 12 is a number

Second run

Please enter a number: -12 -12 is a number

Third run

Please enter a number: 12 12 is a number

Check If a Number is Postive or Negative

While performing arithmetic operations in bash scripting, you should verify the input values. The below shell script will help you to check if the input number is a positive number or a negative number.

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

#!/usr/bin/env bash

# Shell script to check if the input number

# is a positive number or negative number

read p “Please enter a number: “ VAR

## Check if the input is a valid number

if [[ $VAR =~ ^[ ,]?[09] $ ]]; then

   ## Check if the input is a positive or negative number

   if [[ $VAR =~ ^[][09] $ ]]; then

      echo “${VAR} is a negative number”

   else

      echo “${VAR} is a positive number”

   fi

else

   echo “${VAR} is not a number”

fi

Run the above script with positive and negative numbers. Then check the results.

First run

Please enter a number: 12 12 is a positive number

Second run

Please enter a number: -12 -12 is a negative number

Third run

Please enter a number: 12 12 is a positive number

Conclusion

A number is a collection of digits from 0 to 9. Any number can be a positive or negative number. A number can be an integer or floating point number. This tutorial helped you check whether the given value is a number or not in bash scripting.