Open In App

How to Check if a Variable Is a Number in Bash

Last Updated : 02 Jan, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

A number contains a combination of digits from 0-9. All the variables in Bash are treated as character strings but the arithmetic operations and comparisons are allowed even when it is stored in the string format. But before performing any arithmetic operations, we need to check if the stored value is a valid number. To check whether the entered variable is a number, we have to verify that the value contains only digits 

Using regular expression with equal tilde operator (=~)

It is one of the easiest and quickest ways to check if the value is a number or not. The equal tilde (=~) operator is used for comparing the value entered by a user with a regular expression. Bash scripting uses an “if” statement to do the comparison

Script:

echo "Hello World";
read -p "Enter a number: " NUM
 
if [[ $NUM =~ ^[0-9]+$ ]]; then
   echo "${NUM} is a number"
else
   echo "${NUM} is not a number"
fi

Explanation:

We will read the variable value from the user and store it in “NUM”. An equal tilde (=~) is used for comparison to check if the entered value is a number. Print the statements depending on the if-else execution.

Output 1 (Input is a number):

The user is prompted to enter a number using a read. In test case #1 we will enter a number. “If ” returns true and the output is printed as “23456 is a number” 

Script for equal tilde operator

 

Output 2(Input is not a number):

The user is prompted to enter a number using read In test case #2, we will enter a non-numerical value (string, special character, anything other than digits ). “If ” returns false and it goes to else part output is printed as “34@234 is a not number” 

Input is not a number

 

Check using the switch case

To check whether the given output is a number, the alternative way is to use a case statement. It behaves similarly to the switch statement we use in other programming languages. While there are multiple ways to do so, we will use regular expressions in the case statement to verify if a given input is a number or not.

Script:

read -p "Enter a number: " NUM
 
case $NUM in
    ''|*[!0-9]*) echo "${NUM} is not a number" ;;
    *) echo "${NUM} is a number" ;;
esac

Explanation: 

We will read the variable value from the user and store it in “NUM” The case is used for comparison to check if the entered value is a number Validity is checked if the entered number is among the digits (0-9). Print the statements depending on the case execution

Output 1:

The user is prompted to enter a number using read In test case #1 we will enter a number. In the first case of the switch case, the condition holds false (that checks for non-numerical value). It will jump to the second statement and prints “1023” as a number.

Switch-Case (Example 1)

 

Output 2:

The user is prompted to enter a number using a read. In test case #2 we will enter a non-numerical value(having a combination of number and character). Here we can enter any combination of alphabets, digits, special characters, etc. In the first case of the switch case, the condition holds false (that checks for non-numerical value). It will jump to the second statement and prints “1023” as a number.

Switch-Case (Example 2)

 

Matching the real numbers

To find out whether a given number is real or not, simply we can change the rule of the regular expression. We will also add the sign (+ or -) to accept the signed numbers. We will combine the whole expression in a single if statement and check for its validity.

Script:

echo "Hello World";

read -p "Enter a number: " NUM
 
if [[ $NUM =~ ^[+-]?[0-9]+([.][0-9]+)?$ ]]; then
   echo "${NUM} is a real number"
else
   echo "${NUM} is not real number"
fi

Explanation

We will read the variable values from the user and store them in “NUM”. An equal tilde (=~) is used for comparison to check the entered value. We have used regular expressions to find the validity of real numbers. The expression also checks real numbers with a sign (+ or -). Output the result depending upon the input.

Output 1:

The user is prompted to enter a number using a read. In test case #1 we will enter a  “*32.34 ” (which is not a real number). “If ” returns false and jumps to the else part. Output is printed as “*32.34 ” is not a number” 

Matching real numbers (Example 1)

 

Output 2:

The user is prompted to enter a number using a read. In test case #2 we will enter a  “-7448.35 ” (which is a signed real number). “If ” returns true and output is printed as “7448.35 ” is not a number”.

Matching real numbers (Example 2)

 

Conclusion:

The proposed solution checks whether the given input is a number or not. It uses regular expressing using if-else statements or using switch-case execution. 



Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads