mirror of
https://github.com/koalaman/shellcheck.git
synced 2025-10-03 19:29:44 +08:00
Created SC2265 (markdown)
30
SC2265.md
Normal file
30
SC2265.md
Normal file
@@ -0,0 +1,30 @@
|
|||||||
|
## Use && for logical AND. Single & will background and return true.
|
||||||
|
|
||||||
|
### Problematic code:
|
||||||
|
|
||||||
|
```sh
|
||||||
|
if [ "$1" = "install" ] & [ "$USER" != "root" ]
|
||||||
|
then
|
||||||
|
echo "Must be root to install"
|
||||||
|
fi
|
||||||
|
```
|
||||||
|
|
||||||
|
### Correct code:
|
||||||
|
|
||||||
|
```sh
|
||||||
|
if [ "$1" = "install" ] && [ "$USER" != "root" ]
|
||||||
|
then
|
||||||
|
echo "Must be root to install"
|
||||||
|
fi
|
||||||
|
```
|
||||||
|
### Rationale:
|
||||||
|
|
||||||
|
ShellCheck found a `test` command followed by a `&`. This runs the test in the background, effectively ignoring it. To specify "logical AND" between two commands, use `&&`.
|
||||||
|
|
||||||
|
### Exceptions:
|
||||||
|
|
||||||
|
None
|
||||||
|
|
||||||
|
### Related resources:
|
||||||
|
|
||||||
|
* Help by adding links to BashFAQ, StackOverflow, man pages, POSIX, etc!
|
Reference in New Issue
Block a user