mirror of
https://github.com/koalaman/shellcheck.git
synced 2025-10-03 19:29:44 +08:00
Created SC2332 (markdown)
47
SC2332.md
Normal file
47
SC2332.md
Normal file
@@ -0,0 +1,47 @@
|
|||||||
|
## [ ! -o opt ] is always true because -o becomes logical OR. Use [[ ]] or ! [ -o opt ].
|
||||||
|
|
||||||
|
Or "`[ ! -a file ]` is always true because `-a` becomes logical AND. Use `-e` instead."
|
||||||
|
|
||||||
|
|
||||||
|
### Problematic code:
|
||||||
|
|
||||||
|
```sh
|
||||||
|
if [ ! -o braceexpand ]
|
||||||
|
then
|
||||||
|
..
|
||||||
|
fi
|
||||||
|
```
|
||||||
|
|
||||||
|
### Correct code:
|
||||||
|
|
||||||
|
```sh
|
||||||
|
if [[ ! -o braceexpand ]]
|
||||||
|
then
|
||||||
|
..
|
||||||
|
fi
|
||||||
|
```
|
||||||
|
|
||||||
|
or
|
||||||
|
|
||||||
|
```sh
|
||||||
|
if ! [ -o braceexpand ]
|
||||||
|
then
|
||||||
|
..
|
||||||
|
fi
|
||||||
|
```
|
||||||
|
|
||||||
|
### Rationale:
|
||||||
|
|
||||||
|
Bash interprets `[ ! -o opt ]` as `[ "!" ] || [ "opt" ]` instead of negating the condition. As a result, the condition is always true.
|
||||||
|
|
||||||
|
Avoid this by using `[[ ! -o opt ]]` or `! [ -o opt ]`.
|
||||||
|
|
||||||
|
The same issue applies to `[ ! -a file ]`, but this is easier fixed using POSIX standard `[ ! -e file ]`.
|
||||||
|
|
||||||
|
### Exceptions:
|
||||||
|
|
||||||
|
None.
|
||||||
|
|
||||||
|
### Related resources:
|
||||||
|
|
||||||
|
* Help by adding links to BashFAQ, StackOverflow, man pages, POSIX, etc!
|
Reference in New Issue
Block a user