mirror of
https://github.com/koalaman/shellcheck.git
synced 2025-10-03 19:29:44 +08:00
Created SC2283 (markdown)
45
SC2283.md
Normal file
45
SC2283.md
Normal file
@@ -0,0 +1,45 @@
|
|||||||
|
## Use [ ] to compare values, or remove spaces around = to assign (or quote '=' if literal).
|
||||||
|
|
||||||
|
### Problematic code:
|
||||||
|
|
||||||
|
```sh
|
||||||
|
# Assignment
|
||||||
|
var = value
|
||||||
|
|
||||||
|
# Comparison
|
||||||
|
if $var = value
|
||||||
|
then
|
||||||
|
echo "Match"
|
||||||
|
fi
|
||||||
|
```
|
||||||
|
|
||||||
|
### Correct code:
|
||||||
|
|
||||||
|
```sh
|
||||||
|
# Assignment
|
||||||
|
var=value
|
||||||
|
|
||||||
|
# Comparison
|
||||||
|
if [ "$var" = value ]
|
||||||
|
then
|
||||||
|
echo "Match"
|
||||||
|
fi
|
||||||
|
```
|
||||||
|
|
||||||
|
### Rationale:
|
||||||
|
|
||||||
|
ShellCheck found an unquoted `=` after a word.
|
||||||
|
|
||||||
|
If this was supposed to be a comparison, use square brackets: `[ "$var" = value ]`
|
||||||
|
|
||||||
|
If this was supposed to be an assignment, remove spaces around `=`: `var=value`
|
||||||
|
|
||||||
|
### Exceptions:
|
||||||
|
|
||||||
|
If the `=` was meant literally, quote it:
|
||||||
|
|
||||||
|
grep '=true' file.cfg
|
||||||
|
|
||||||
|
### Related resources:
|
||||||
|
|
||||||
|
* Help by adding links to BashFAQ, StackOverflow, man pages, POSIX, etc!
|
Reference in New Issue
Block a user