mirror of
https://github.com/koalaman/shellcheck.git
synced 2025-10-03 11:19:45 +08:00
Created SC2050 (markdown)
33
SC2050.md
Normal file
33
SC2050.md
Normal file
@@ -0,0 +1,33 @@
|
||||
## This expression is constant. Did you forget the `$` on a variable?
|
||||
### Problematic code:
|
||||
|
||||
```sh
|
||||
if [ myvar = "test" ]
|
||||
then
|
||||
echo "Test mode"
|
||||
fi
|
||||
```
|
||||
|
||||
### Correct code:
|
||||
|
||||
```sh
|
||||
if [ "$myvar" = "test" ]
|
||||
then
|
||||
echo "Test mode"
|
||||
fi
|
||||
```
|
||||
### Rationale:
|
||||
|
||||
ShellCheck has found a `[ .. ]` or `[[ .. ]]` comparison that only involves literal strings. The intention was probably to check a variable or command output instead.
|
||||
|
||||
This is usually due to missing `$` or bad quoting:
|
||||
|
||||
if [[ "myvar" = "test" ]] # always false because myvar is a literal string
|
||||
if [[ "$myvar" = "test" ]] # correctly compares a variable
|
||||
|
||||
if [ 'grep -c foo bar' -ge 10 ] # always false because grep doesn't run
|
||||
if [ "$(grep -c foo bar)" -ge 10 ] # correctly checks grep output
|
||||
|
||||
### Exceptions:
|
||||
|
||||
None
|
Reference in New Issue
Block a user