Facebook Twitter Instagram
    TecAdmin
    • Home
    • FeedBack
    • Submit Article
    • About Us
    Facebook Twitter Instagram
    TecAdmin
    You are at:Home»Programming»Bash Shell»Check if a Variable Contains a Number in Bash

    Check if a Variable Contains a Number in Bash

    By RahulOctober 21, 20224 Mins Read

    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.

    Advertisement

    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:

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    #!/usr/bin/env bash
    # Shell script to check if the input number is an integer
     
    read -p "Please enter a number: " VAR
     
    if [[ $VAR =~ ^[0-9]+$ ]]; 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.

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    #!/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 =~ ^[0-9]+([.][0-9]+)?$ ]]; 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.

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    #!/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
        ''|*[!0-9]*) 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.

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    #!/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 =~ ^[+-]?[0-9]+$ ]]; 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 =~ ^[+,-]?[0-9]+$ ]]; then
       ## Check if the input is a positive or negative number
       if [[ $VAR =~ ^[-][0-9]+$ ]]; 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.

    bash if-else validation
    Share. Facebook Twitter Pinterest LinkedIn Tumblr Email WhatsApp

    Related Posts

    10 Most Popular Linux Shells

    10 Most Popular Open Source Linux Shells

    How to Create Bash Aliases with Arguments

    How to Create Bash Aliases with Parameters

    Checking If a Command Succeeded in Bash Using the `$?` Special Variable

    Add A Comment

    Leave A Reply Cancel Reply

    Advertisement
    Recent Posts
    • How to Install PHP 8.2-7.4 on RHEL & CentOS Stream 9
    • How to Install MySQL 8.0 on RHEL & CentOS Stream 9
    • How to Split Large Archives in Linux using the Command Line
    • System.out.println() Method in Java: A Beginner’s Guide
    • Split Command in Linux With Examples (Split Large Files)
    Facebook Twitter Instagram Pinterest
    © 2023 Tecadmin.net. All Rights Reserved | Terms  | Privacy Policy

    Type above and press Enter to search. Press Esc to cancel.