Glam Prestige Journal

Bright entertainment trends with youth appeal.

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?

1 Answer

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]*
123
$ 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
12
$ 

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

6

Your Answer

Sign up or log in

Sign up using Google Sign up using Facebook Sign up using Email and Password

Post as a guest

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy