mirror of
https://github.com/koalaman/shellcheck.git
synced 2025-10-03 19:29:44 +08:00
Created SC1139 (markdown)
36
SC1139.md
Normal file
36
SC1139.md
Normal file
@@ -0,0 +1,36 @@
|
|||||||
|
## Use || instead of '-o' between test commands.
|
||||||
|
|
||||||
|
And variations, like "Use `&&` instead of `and`".
|
||||||
|
|
||||||
|
### Problematic code:
|
||||||
|
|
||||||
|
```sh
|
||||||
|
if [ "$1" = "-v" ] -o [ -n "$VERBOSE" ]
|
||||||
|
then
|
||||||
|
echo "Verbose log"
|
||||||
|
fi
|
||||||
|
```
|
||||||
|
|
||||||
|
### Correct code:
|
||||||
|
|
||||||
|
```sh
|
||||||
|
if [ "$1" = "-v" ] || [ -n "$VERBOSE" ]
|
||||||
|
then
|
||||||
|
echo "Verbose log"
|
||||||
|
fi
|
||||||
|
```
|
||||||
|
### Rationale:
|
||||||
|
|
||||||
|
You have a `[ .. ]` or `[[ .. ]]` test expression followed by `-o`/`-a` (or by Python-style `or`/`and`).
|
||||||
|
|
||||||
|
`-o` and `-a` work *inside* `[ .. ]`, but they do not work *between* them. The Python operators `or` and `and` are never recognized in Bash.
|
||||||
|
|
||||||
|
To join two separate test expressions, instead use `||` for "logical OR", or `&&` for "logical AND".
|
||||||
|
|
||||||
|
### Exceptions:
|
||||||
|
|
||||||
|
None
|
||||||
|
|
||||||
|
### Related resources:
|
||||||
|
|
||||||
|
* Help by adding links to BashFAQ, StackOverflow, man pages, POSIX, etc!
|
Reference in New Issue
Block a user