Skip to content

Files

Latest commit

 

History

History
29 lines (17 loc) · 1.02 KB

SC2076.md

File metadata and controls

29 lines (17 loc) · 1.02 KB

Pattern: Use of quotes for right side of =~

Issue: -

Description

Quotes on the right hand side of =~ can be used to match literally, so that [[ $1 =~ ^"$2".* ]] works even if $2 contains regex meta-characters. This mirrors the behavior of globs, [[ $1 = "$2"* ]].

This also means that the problematic code tries to match literal carets and plus signs instead of interpreting them as regular expression matchers. To match as a regex, the regex meta-characters it must be unquoted. Literal parts of the expression can be quoted with double or single quotes, or escaped.

Example of incorrect code:

[[ $foo =~ "^fo+ bar$" ]]

Example of correct code:

[[ $foo =~ ^fo+\ bar$ ]]

Exceptions

If you do want to match literally just to do a plain substring search, e.g. [[ $foo =~ "bar" ]], you could ignore this message, but consider using a more canonical glob match instead: [[ $foo = *"bar"* ]].

Further Reading