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
Non-linear Components In electrical circuits, Non-linear Components are electronic devices that need an external power source to operate actively. Non-Linear Components are those that are changed with respect to the voltage and current. Elements that do not follow ohm's law are called Non-linear Components. Non-linear Co
11 min read
Spring Boot Tutorial Spring Boot is a Java framework that makes it easier to create and run Java applications. It simplifies the configuration and setup process, allowing developers to focus more on writing code for their applications. This Spring Boot Tutorial is a comprehensive guide that covers both basic and advance
10 min read
Class Diagram | Unified Modeling Language (UML) A UML class diagram is a visual tool that represents the structure of a system by showing its classes, attributes, methods, and the relationships between them. It helps everyone involved in a projectâlike developers and designersâunderstand how the system is organized and how its components interact
12 min read
3-Phase Inverter An inverter is a fundamental electrical device designed primarily for the conversion of direct current into alternating current . This versatile device , also known as a variable frequency drive , plays a vital role in a wide range of applications , including variable frequency drives and high power
13 min read
Backpropagation in Neural Network Back Propagation is also known as "Backward Propagation of Errors" is a method used to train neural network . Its goal is to reduce the difference between the modelâs predicted output and the actual output by adjusting the weights and biases in the network.It works iteratively to adjust weights and
9 min read
Decorators in Python In Python, decorators are a powerful and flexible way to modify or extend the behavior of functions or methods, without changing their actual code. A decorator is essentially a function that takes another function as an argument and returns a new function with enhanced functionality. Decorators are
10 min read
What is Vacuum Circuit Breaker? A vacuum circuit breaker is a type of breaker that utilizes a vacuum as the medium to extinguish electrical arcs. Within this circuit breaker, there is a vacuum interrupter that houses the stationary and mobile contacts in a permanently sealed enclosure. When the contacts are separated in a high vac
13 min read
Linux Commands Cheat Sheet Linux, often associated with being a complex operating system primarily used by developers, may not necessarily fit that description entirely. While it can initially appear challenging for beginners, once you immerse yourself in the Linux world, you may find it difficult to return to your previous W
13 min read
Polymorphism in Java Polymorphism in Java is one of the core concepts in object-oriented programming (OOP) that allows objects to behave differently based on their specific class type. The word polymorphism means having many forms, and it comes from the Greek words poly (many) and morph (forms), this means one entity ca
7 min read
Linux/Unix Tutorial Linux is one of the most widely used open-source operating systems. It's fast, secure, stable, and powers everything from smartphones and servers to cloud platforms and IoT devices. Linux is especially popular among developers, system administrators, and DevOps professionals.Linux is:A Unix-like OS
10 min read