mirror of
https://github.com/koalaman/shellcheck.git
synced 2025-10-03 19:29:44 +08:00
Created SC3015 (markdown)
29
SC3015.md
Normal file
29
SC3015.md
Normal file
@@ -0,0 +1,29 @@
|
|||||||
|
## In POSIX sh, =~ regex matching is undefined.
|
||||||
|
|
||||||
|
### Problematic code:
|
||||||
|
|
||||||
|
```sh
|
||||||
|
[ "$var" =~ .*foo[0-9]* ]
|
||||||
|
```
|
||||||
|
|
||||||
|
### Correct code:
|
||||||
|
|
||||||
|
```sh
|
||||||
|
expr "$var" : ".*foo[0-9]*" > /dev/null
|
||||||
|
```
|
||||||
|
|
||||||
|
### Rationale:
|
||||||
|
|
||||||
|
You are using `=~` in a script declared to be compatible with POSIX sh or Dash.
|
||||||
|
|
||||||
|
`=~` is not a POSIX operator and is unlikely to outside `[[ ]]` in Bash and Ksh.
|
||||||
|
|
||||||
|
Use `expr`'s `:` operator instead.
|
||||||
|
|
||||||
|
### Exceptions:
|
||||||
|
|
||||||
|
None
|
||||||
|
|
||||||
|
### Related resources:
|
||||||
|
|
||||||
|
* Help by adding links to BashFAQ, StackOverflow, man pages, POSIX, etc!
|
Reference in New Issue
Block a user