echo file|grep fil* fails, but
echo abcd|grep abc* succeeds.
I don't understand it, can someone explain?
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. It only takes a minute to sign up.Sign up to join this community
There are two problems with your example.
The primary one is that you're assuming that regular expressions work the same as glob patterns in that
* is a wildcard meaning "any sequence of characters." In regular expressions,
* means "any number of the previous atom" instead, so
f followed by
i followed by zero or more
l characters. You need to say
grep fil.* to get the intended meaning:
. means "any single character, so that
.* means "any sequence of characters."
The lesser problem is that you're using unquoted special characters that mean something under glob rules, which means the shell could interpret them. If you had any files in the local directory matching the glob patterns
abc*, the shell would expand them, so
grep would get the expanded file names as a pattern, not the intended RE. Whenever you're using such characters on the command line, you should quote them:
echo file | grep 'fil.*'.