mirror of
https://github.com/koalaman/shellcheck.git
synced 2025-10-03 19:29:44 +08:00
Updated SC2076 (markdown)
@@ -3,20 +3,20 @@
|
||||
### Problematic code:
|
||||
|
||||
```sh
|
||||
[[ $foo =~ "^fo+bar$" ]]
|
||||
[[ $foo =~ "^fo+ bar$" ]]
|
||||
```
|
||||
|
||||
### Correct code:
|
||||
|
||||
```sh
|
||||
[[ $foo =~ ^fo+bar$ ]]
|
||||
[[ $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.
|
||||
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 metacharacters it must be unquoted. Literal parts of the expression can be quoted with double or single quotes, or escaped.
|
||||
|
||||
### Exceptions:
|
||||
|
||||
|
Reference in New Issue
Block a user