8

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?

ocodo
  • 29,401
  • 18
  • 105
  • 117
Gray Race
  • 195
  • 1
  • 1
  • 5
  • You should edit your question to include why you think it isn't working. are you getting to the else statment? or are you getting a syntax error. Good luck. – shellter Sep 15 '11 at 23:35
  • 3
    Regexp in Bash (v3 and later) doesn't support `\d`, you can use `[0-9]` or the `[:digit:]` posix character class. – ocodo Sep 15 '11 at 23:45
  • 1
    The fully portable way would be to use a glob pattern, i.e. `case $id in [0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9]) echo fine ;; *) die in anguish;; esac` – tripleee Sep 16 '11 at 07:27

4 Answers4

9

As others already answered you should use [0-9]{9} because \d is not supported. But another important part is, you have to use anchors!

^[0-9]{9}$

The ^ anchors the regex to the start of the string and the $ anchors it to the end. If you don't use them your regex will match on every string that contains 9 digits in a sequence, like "abc123456789", "asdf123456789zui" or "123456789FOOBAR".

stema
  • 90,351
  • 20
  • 107
  • 135
9

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.

David W.
  • 105,218
  • 39
  • 216
  • 337
  • I don't know what I was smoking earlier, but I just tried it out now and it works. I could have sworn that it didn't work when I tried it the last time because it was one of the first things I tried. – Vivin Paliath Sep 16 '11 at 14:34
  • This is what I was looking for thank you. Do you have a reference article for finding equvalancies between items such as /d & [[:digit:]] Or are the man pages the best resource? – Gray Race Sep 16 '11 at 15:37
  • Actually, the best source is the Perl documentation. After all, all of the extended regular expression stuff originally came from Perl. Take a look at [perldoc perlre](http://perldoc.perl.org/perlreref.html#CHARACTER-CLASSES) and the part about the character classes. – David W. Sep 16 '11 at 17:28
  • great thanks ! how would you specify a number up to 9 digits though? – Bluz Nov 08 '13 at 16:36
3

Try this:

if [[ $id =~ [0-9]{9} ]]; then

It looks like bash doesn't recognize \d as [0-9].

Bash uses the Extended Regular Expression dialect, which doesn't support \d.

According to ERE's grammar (lexical conventions), escaped characters are of the form \SPEC_CHAR. SPEC_CHAR can be any one of the following:

^    .    [    $    (    )    |
*    +    ?    {    \
Vivin Paliath
  • 94,126
  • 40
  • 223
  • 295
-2

The problem is \d gets turned into d before it's interpreted. Try double escaping.

\d{9} => d{9}
\\d{9} => \d{9}

Alternatively, you could put it in quotes

if [[ $id =~ "\d{9}" ]]
Asmor
  • 5,043
  • 6
  • 32
  • 42