Ask Ubuntu is a question and answer site for Ubuntu users and developers. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I have several Ubuntu servers and there is some behavior that has me curious.

On any newly installed machine (Ubuntu Server 12.04.3 LTS), if I run:

echo "testbla123" | grep -P -o [0-9]*

This will return 123 as expected.

On an older machine (Ubuntu Server 12.04.1 LTS), if I run the same command, I get nothing back, I have to instead run:

echo "testbla123" | grep -P -o "[0-9]*"

After performing an apt-get upgrade, it will perform without quotes around the regex.

Grep is at the same versions on both machines. I tried comparing dependencies before and after the update for some time without much luck.

I was wondering if anyone knows why this happens?

share|improve this question

Chances are you have a file that matches the glob pattern [0-9]* on your second machine, and not on the first one. If that's the case, the shell will expand the glob into the file's name before passing that as an argument to grep.

If there is no file that match on the current directory, the shell leaves the glob pattern untouched and passes it as-is to the command.

$ echo abc123 | grep -P -o [0-9]*
$ touch 234
$ echo abc123 | grep -P -o [0-9]* # shell runs grep -P -o 234
$ touch 123
$ echo abc123 | grep -P -o [0-9]* # shell runs grep -P -o 123 234
$ rm 234 123
$ touch 12
$ echo abc123 | grep -P -o [0-9]* # shell runs grep -P -o 12

Always quote parameters that contain shell wildcards if you don't want them to be expanded by the shell.

share|improve this answer
I understand what you are saying and will continue to do it in quotes, however, the moment that I perform an update on the older machines, it works... I really want an explanation on what update could have caused a syntax change when Grep itself remains the same version. – wilhil Aug 22 '13 at 11:31
Are you 100% certain you ran those commands in the same directory before and after the upgrade? Was the shell upgraded? – Mat Aug 22 '13 at 11:32
The syntax of Grep has never changed. As Mat explained, the problem here is not grep, but it's the shell which is doing "too much". Instead of echo something | grep expr, try just echo expr. Or maybe try running set -x before typing any command. – Andrea Corbellini Aug 22 '13 at 12:37
@Mat - Yes I am – wilhil Aug 23 '13 at 13:05
@AndreaCorbellini - I fully understand that, and as I said, it was the same Grep version on both machines, it suddenly started working after doing an update - I am certain it was something shell or dependency related, I am just not sure what and that is what I would like to know! – wilhil Aug 23 '13 at 13:06

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.