1
0
mirror of https://github.com/koalaman/shellcheck.git synced 2025-01-04 01:59:56 -08:00

Created SC2076 (markdown)

koalaman 2015-08-15 22:55:11 -07:00
parent 7479eb071f
commit e7010b4643

19
SC2076.md Normal file

@ -0,0 +1,19 @@
## Don't quote rhs of =~, it'll match literally rather than as a regex.
### Problematic code:
[[ $foo =~ "^fo+bar$" ]]
### Correct code:
[[ $foo =~ ^fo+bar$ ]]
### Rationale:
Quotes on the right hand side of `=~` can be used to match literally, so that `[[ $1 =~ ^"$2".* ]]` works even if `$2` contains regex metacharacters. 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, it must be unquoted.
### 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"* ]]`.