notation (.*)
The * in the regular expressions .* and * is referring to a count, not characters per say, more exactly it means 'zero or more'. Furthermore, the . means 'any single character'.
So when you put them together you get 'zero or more of any characters'. For example strings like these:
- linux
- linnnnnx
- lnx
- hi linux
- lx
Would be matched by <l.*x>
. The last one is important, it shows that the .* can match nothing too.
notation (*)
The use of * alone as I said is a counter. So when you put it after a letter such as 'l' the * is saying 'zero or more of l'.
Notice if we grep for l*x
, this will match l...x
, but probably not for the reason you'd think.
% echo "l...x" | grep "l*x"
l...x
It's matching on the trailing 'x'. The 'l' has nothing to do with why this is getting matched, other than the fact that the 'x' is preceded by 'zero or more l's'.
As choroba said, sed will always print the line, by default, with any substitutions that matched. You could do what you want with:
regex="ss"
string="blossom"
echo $string | sed -n "s/^.*\($regex\).*$/\1/p"
The -n tells sed not to print the line, then the p at the end of the s/ command tells sed to print the line, with replacements, if it matched anything.
Best Answer
The binary operator, ‘=~’, has the same precedence as ‘==’ and ‘!=’. When it is used, the string to the right of the operator is considered an extended regular expression and matched accordingly (as in regex3)). The return value is 0 if the string matches the pattern, and 1 otherwise. If the regular expression is syntactically incorrect, the conditional expression’s return value is 2.
From: bash
Hence your comparing for equality versus for a regular expression match.