What Does ‘^[0-9]+$’ Mean in Bash?

In Bash, the regular expression ‘^[0-9]+$’ matches any string that consists of one or more digits from the beginning to the end of the line with no other characters in between. This expression is beneficial to test whether a variable has numeric characters or not.

This guide will illustrate the meaning of the “^[0-9]+$” with practical implementation in bash.

What Does ‘^[0-9]+$’ Mean in Bash?

Here is a breakdown of the different parts of the “^[0-9]+$” regular expression:

  • ^: It is the start of the string anchor that compares the start of the line.
  • [0-9]: It is a character class that consists of digits (0-9).
  • +: It matches one or more digits between 0 and 9.
  • $: It is the end of the string anchor that matches the end of the line.
  • The complete regular expression considers the characters from start to end.

How Does ‘^[0-9]+$’ Work in Bash?

To explore how the regular expression ‘^[0-9]+$’ can be used in Bash, follow the mentioned examples:

Example 1: Testing if a Variable Contains Only Digits

The following if-else statement is utilized to check the existence of the digits in the variable: 

#!/bin/bash
var="1234"
if [[ $var =~ ^[0-9]+$ ]]; then
    echo "Variable contains only digits."
else
    echo "Variable contains non-digit characters."
fi

In this example, the variable ‘var’ is set to “1234“. The regular expression ‘^[0-9]+$’ is utilized to test whether a variable contains only numeric characters. 

Save and close the script. After that, acquire the executable permission using the “chmod” command with the “+x” option and execute the ./script.sh file:

$ sudo chmod +x script.sh
$ ./script.sh

The regular expression ‘^[0-9]+$’ matches the string “1234” since it contains only digits, so the output is “Variable contains only digits.”

Example 2: Restricting Input to a Script to Only Digits

Another example is considered to take input from the user and check the input value using the regular expression ‘^[0-9]+$‘:

#!/bin/bash
read -p "Enter a number: " input
if [[ $input =~ ^[0-9]+$ ]]; then
    echo "Input contains only digits."
else
    echo "Input contains non-digit characters."
fi

In the above code, the user is prompted to enter a number using the “read” command. After that, the regular expression ‘^[0-9]+$‘ validates the input, ensuring that it only contains digits. If the input contains non-digit characters, the script displays the output “Input contains non-digit characters.”

Save and close the script. Then, acquire the executable permission using the “chmod” command with the “+x” option and run the ./script.sh file:

$ sudo chmod +x script.sh
$ ./script.sh

After inputting “006” digit characters, the output displays “Input contains only digits.”

Conclusion

In Bash, the regular expression ‘^[0-9]+$‘ matches a string that contains only one or more digits (0-9) and no other characters. Using this expression, users can restrict the input value/predefined value to the numeric characters after evaluating the variable. This guide has explained the ‘^[0-9]+$‘ regular expression along with possible examples in Bash.