From Bash Manual
Storing the regular expression in a shell variable is often a useful way to avoid problems with quoting characters that are special to the
shell. It is sometimes difficult to specify a regular expression
literally without using quotes, or to keep track of the quoting used
by regular expressions while paying attention to the shell’s quote
removal. Using a shell variable to store the pattern decreases these
problems. For example, the following are equivalent:pattern='[[:space:]]*(a)?b' [[ $line =~ $pattern ]]
and
[[ $line =~ [[:space:]]*(a)?b ]]
If you want to match a character that’s special to the regular
expression grammar, it has to be quoted to remove its special meaning.
This means that in the patternxxx.txt
, the.
matches any
character in the string (its usual regular expression meaning), but in
the pattern"xxx.txt"
it can only match a literal.
. Shell
programmers should take special care with backslashes, since
back-slashes are used both by the shell and regular expressions to
remove the special meaning from the following character. The following
two sets of commands are not equivalent:pattern='.' [[ . =~ $pattern ]] [[ . =~ . ]] [[ . =~ "$pattern" ]] [[ . =~ '.' ]]
The first two matches will succeed, but the second two will not,
because in the second two the backslash will be part of the pattern to
be matched. In the first two examples, the backslash removes the
special meaning from.
, so the literal.
matches. If the string in
the first examples were anything other than.
, saya
, the pattern
would not match, because the quoted.
in the pattern loses its
special meaning of matching any single character.
How is storing the regular expression in a shell variable a useful way to avoid problems with quoting characters that are special to the shell?
The given examples don’t seem to explain that.
In the given examples, the regex literals in one method and the values of the shell variable pattern
in the other method are the same.
Thanks.