mirror of
https://github.com/koalaman/shellcheck.git
synced 2025-10-03 19:29:44 +08:00
Created SC2074 (markdown)
22
SC2074.md
Normal file
22
SC2074.md
Normal file
@@ -0,0 +1,22 @@
|
||||
## Can't use `=~` in `[ ]`. Use `[[..]]` instead.
|
||||
|
||||
### Problematic code:
|
||||
|
||||
```sh
|
||||
[ "$input" =~ DOC[0-9]*\.txt ] && echo "match"
|
||||
```
|
||||
|
||||
### Correct code:
|
||||
|
||||
```sh
|
||||
[[ "$input" =~ DOC[0-9]*\.txt ]] && echo "match"
|
||||
```
|
||||
### Rationale:
|
||||
|
||||
`=~` only works in `[[ .. ]]` tests. It does not work with `test` or `[` in any shell.
|
||||
|
||||
If you're targeting POSIX `sh`, rewrite in terms of `case` or `grep` instead.
|
||||
|
||||
### Exceptions:
|
||||
|
||||
None.
|
Reference in New Issue
Block a user