1

In the following code, I'm getting an error and I'm not sure why:

[name@unix ~]$cat test123
123
456
789
1011
1213
[name@unix ~]$egrep ^[0-9]{1,3}$ test123
egrep: ^[0-9]3$: No such file or directory
[name@unix ~]$egrep ^[0-9]{3}$ test123
123
456
789
[name@unix ~]$

When I do $man egrep, I'm shown this:

{n} The preceding item is matched exactly n times. {n,} The preceding item is matched n or more times. {n,m} The preceding item is matched at least n times, but not more than m times.

1 Answer 1

1

Your shell is interpreting {1,3} as a brace expansion, resulting in grep seeing

grep ^[0-9]1$ ^[0-9]3$ test123

as a result of which it takes ^[0-9]3$ to be an additional filename argument. You should always quote your regex to prevent such expansion by the shell i.e.

egrep '^[0-9]{1,3}$' test123
1
  • Thank you! That makes perfect sense but seems non-intuitive for some reason.
    – Ben
    Commented Apr 8, 2015 at 23:52

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .