I'm unable to get a pattern to match properly using regex in bash 4.1. I've read some information on about differences in quotes vs not quotes but I don't think that is my problem.
My goal is to check and make sure that the script is provided a valid ID. A valid ID in this case is a string of 9 digits. It's my understand that a regex expression for that is \d{9}. Given that here is my code snippet to check:
id=$1
if [[ $id =~ \d{9} ]]; then
echo "This is a vaild ID"
else
echo "This is not a vaild ID"
fi
Then call the script with:
./script 987654321
Something obvious that I am missing?
This will work: if [[ $id =~ [[:digit:]]{9} ]] – David W. 11 hours ago
@David I tried that on bash and it didn't seem to work. – Vivin Paliath 10 hours ago
I've just written a test program:
#! /bin/bash
for id in 123456789 12345689 1234567890 987654321
do
if [[ $id =~ ^[[:digit:]]{9}$ ]]
then
echo "$id is 9 digits long"
else
echo "$id is bad"
fi
done
And I got the following output:
DaveBook:~ david$ ./test.sh
123456789 is 9 digits long
12345689 is bad
1234567890 is bad
987654321 is 9 digits long
DaveBook:~ david$
I'm using BASH 3.2.48 for Mac OS X and Bash 4.1.10(4) for Cygwin (Wow, the Mac version is that old?). I haven't tested this on a Linux box.
What version are you using? Are you doubling the square braces around :digit:
? It has to be [[:digit:]]
and not [:digit:]
.
I also realized I need the ^
and $
anchors in there because you don't want to match foo123456789bar
or 1234567890123456790
. Was this an issue you had?
The thing is that [[:digit:]]
should have worked. I've used it in many Bash shell scripts thinking it was fairly universal. However, if you have a BASH shell where it doesn't work, I'm going to have to stop using it.