Test for an argument that is a 5-digit integer

I want to check if the argument in my bash

script is a 5 digit integer.

if [ "$1" == [0-9][0-9][0-9][0-9][0-9] ]; then
    echo "first arg is 5 digits"
else
    echo "not 5 digits"

      

This doesn't work for me, is there an easier way to do this using a function expr

?

+3


source to share


3 answers


See CONDITIONAL EXPRESSIONS

in man bash

:



if [[ "$1" =~ ^[0-9]{5}$ ]]; then
    echo "first arg is 5 digits"
else
    echo "not 5 digits"
fi

      

+4


source


You must use double parentheses when testing such an expression.



+4


source


case "$1" in
  [0-9][0-9][0-9][0-9][0-9]) echo "first arg is 5 digits" ;;
  *) echo "not 5 digits" ;;
esac

      

0


source







All Articles