mirror of
https://github.com/koalaman/shellcheck.git
synced 2025-01-07 03:29:56 -08:00
Created SC2161 (markdown)
parent
8f26f5a02c
commit
d04cfea0a2
29
SC2161.md
Normal file
29
SC2161.md
Normal file
@ -0,0 +1,29 @@
|
||||
## Instead of '[ 1 ]', use 'true'.
|
||||
|
||||
### Problematic code:
|
||||
|
||||
while [ 1 ]
|
||||
do
|
||||
echo "infinite loop"
|
||||
done
|
||||
|
||||
### Correct code:
|
||||
|
||||
while true
|
||||
do
|
||||
echo "infinite loop"
|
||||
done
|
||||
|
||||
### Rationale:
|
||||
|
||||
This is a stylistic suggestion to use `true` instead of `[ 1 ]`.
|
||||
|
||||
`[ 1 ]` seems to suggest that the value "1" is somehow relevant to the statement. This is not the case: it doesn't matter. You can replace it with `[ 0 ]` or `[ wombat ]`, and it will still always be true.
|
||||
|
||||
If you instead use `true`, the value is actually considered and can be inverted by replacing with `false`.
|
||||
|
||||
On bash, you can also use `(( 1 ))`, which evaluates to true much like in C. `(( 0 ))` is similarly false.
|
||||
|
||||
### Exceptions:
|
||||
|
||||
None.
|
Loading…
Reference in New Issue
Block a user