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
user1813619
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
Anton Kovalenko
source
to share
You must use double parentheses when testing such an expression.
+4
michaelmeyer
source
to share
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
wnoise
source
to share