mirror of
https://github.com/koalaman/shellcheck.git
synced 2025-10-03 19:29:44 +08:00
Created SC2195 (markdown)
36
SC2195.md
Normal file
36
SC2195.md
Normal file
@@ -0,0 +1,36 @@
|
|||||||
|
## This pattern will never match the case statement's word. Double check them.
|
||||||
|
|
||||||
|
### Problematic code:
|
||||||
|
|
||||||
|
```sh
|
||||||
|
case "$var " of # Trailing space
|
||||||
|
value) echo "Match"
|
||||||
|
esac
|
||||||
|
```
|
||||||
|
|
||||||
|
### Correct code:
|
||||||
|
|
||||||
|
```sh
|
||||||
|
case "${var}" of # No trailing space
|
||||||
|
value) echo "Match"
|
||||||
|
esac
|
||||||
|
```
|
||||||
|
### Rationale:
|
||||||
|
|
||||||
|
ShellCheck has detected that one of the patterns in a `case` statement will never match.
|
||||||
|
|
||||||
|
Often, this is due to mistaes in the case statement word that results in unintended literal characters. In the problematic code, there's a trailing space that will prevent the match from ever happening.
|
||||||
|
|
||||||
|
For more examples of when this could happen, see [SC2193](https://github.com/koalaman/shellcheck/wiki/SC2193) for the equivalent warning for `[[ .. ]]` statements.
|
||||||
|
|
||||||
|
Note that ShellCheck warns about individual patterns in a branch, and will flag `*.png` in this example even though the branch is not dead:
|
||||||
|
|
||||||
|
```
|
||||||
|
case "${img}.jpg" in
|
||||||
|
*.png | *.jpg) echo "It's an image"
|
||||||
|
esac
|
||||||
|
```
|
||||||
|
|
||||||
|
### Exceptions:
|
||||||
|
|
||||||
|
None. If you encounter a bug and wish to [[ignore]] this warning, make sure the directive goes in front of the `case` and not the individual branch.
|
Reference in New Issue
Block a user