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 a variable is an array or not in PHP ?
There are two ways for checking whether the variable is an array or not. We can check whether a variable is an array or not by using the PHP is_array() function and by casting the variable to the array. Approach 1: We can check whether a variable is an array or not using the is_array() function. The
2 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 Prime or not
Given a number, the task is to find whether the given number is prime or not using Bash Scripting. Examples: Input: N = 43 Output: Prime Input: N = 35 Output: Not Prime Prime Numbers: A prime number is a whole number greater than 1, which is only divisible by 1 and itself. First few prime numbers ar
1 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
How to check if an Array contains a value or not?
There are many ways for checking whether the array contains any specific value or not, one of them is: Examples: Input: arr[] = {10, 30, 15, 17, 39, 13}, key = 17Output: True Input: arr[] = {3, 2, 1, 7, 10, 13}, key = 20Output: False Approach: Using in-built functions: In C language there is no in-b
3 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 check object is an array in JavaScript ?
There are two different approaches to check an object is an array or not in JavaScript. 1. Using Array.isArray() MethodThe Array.isArray() method determines whether the value passed to this function is an array or not. This method returns true if the argument passed is an array else it returns false
1 min read