How to Check if a Variable Is a Number in Bash
Last Updated :
02 Jan, 2023
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"

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"

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.

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.

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"

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

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.
Similar Reads
How to Check if a Variable is an Array in JavaScript?
To check if a variable is an array, we can use the JavaScript isArray() method. It is a very basic method to check a variable is an array or not. Checking if a variable is an array in JavaScript is essential for accurately handling data, as arrays have unique behaviors and methods. Using JavaScript
2 min read
Bash Scripting - How to check If variable is Set
In BASH, we have the ability to check if a variable is set or not. We can use a couple of arguments in the conditional statements to check whether the variable has a value. Â In this article, we will see how to check if the variable has a set/empty value using certain options like -z, -v, and -n. Usi
10 min read
How to Check the Data Type of a Variable in Ruby?
In this article, we will discuss how to check the data type of a variable in Ruby. We can check the data types of a variable through different methods ranging from class to kind_of method in Ruby. Table of Content Using Class method Using is_a? Method Using kind_of? MethodUsing Class method The Clas
3 min read
Bash program to check if the Number is a Palindrome
Given a number num, find whether the given number is palindrome or not using Bash Scripting. Examples: Input : 666 Output : Number is palindrome Input : 45667 Output : Number is NOT palindrome Approach To find the given number is palindrome just check if the number is same from beginning and the end
1 min read
Bash Script - Define Bash Variables and its types
Variables are an important aspect of any programming language. Without variables, you will not be able to store any required data. With the help of variables, data is stored at a particular memory address and then it can be accessed as well as modified when required. In other words, variables let yo
12 min read
How to Check the Syntax of a Bash Script Without Running It?
A bash script is a text file that contains a sequence of commands that are executed by the bash shell, a Unix-based command-line interface. Bash scripts are used to automate tasks, create utility scripts, and perform a wide range of other functions in the command-line environment. They can include v
5 min read
How to check the type of a variable or object in JavaScript ?
In this article, How to Check the Type of a Variable or Object in JavaScript? In JavaScript, the typeof operator is used to determine the typeof an object or variable. JavaScript, on the other hand, is a dynamically typed (or weakly typed) language. This indicates that a variable can have any type o
2 min read
Check If Value Is Int or Float in Python
In Python, you might want to see if a number is a whole number (integer) or a decimal (float). Python has built-in functions to make this easy. There are simple ones like type() and more advanced ones like isinstance(). In this article, we'll explore different ways to check if a value is an integer
4 min read
How to Validate Decimal Numbers in JavaScript ?
Validating user input is an essential aspect of Web Development. As a developer, when we are playing with the numeric inputs provided by the end-user, it is quite important to ensure that the input provided by the user is in the correct format. We can use the regular expression to Validate Decimal N
2 min read
JavaScript Program to Check if a Number is Float or Integer
In this article, we will see how to check whether a number is a float or an integer in JavaScript. A float is a number with a decimal point, while an integer is a whole number or a natural number without having a decimal point. Table of ContentUsing the Number.isInteger() MethodUsing the Modulus Ope
2 min read