mirror of
https://github.com/koalaman/shellcheck.git
synced 2025-10-03 19:29:44 +08:00
Created SC2236 (markdown)
37
SC2236.md
Normal file
37
SC2236.md
Normal file
@@ -0,0 +1,37 @@
|
|||||||
|
## Use `-n` instead of `! -z`.
|
||||||
|
|
||||||
|
(or "Use `-z` instead of `! -n`)
|
||||||
|
|
||||||
|
### Problematic code:
|
||||||
|
|
||||||
|
```sh
|
||||||
|
if [ ! -n "$JAVA_HOME" ]; then echo "JAVA_HOME not specified"; fi
|
||||||
|
if [ ! -z "$STY" ]; then echo "You are already running screen"; fi
|
||||||
|
```
|
||||||
|
|
||||||
|
### Correct code:
|
||||||
|
|
||||||
|
```sh
|
||||||
|
if [ -z "$JAVA_HOME" ]; then echo "JAVA_HOME not specified"; fi
|
||||||
|
if [ -n "$STY" ]; then echo "You are already running screen"; fi
|
||||||
|
```
|
||||||
|
|
||||||
|
### Rationale:
|
||||||
|
|
||||||
|
You have negated `test -z` or `test -n`, resulting in a needless double-negative. You can just use the other operator instead:
|
||||||
|
|
||||||
|
# Identical tests to verify that a value is assigned
|
||||||
|
[ ! -z foo ] # Not has no value
|
||||||
|
[ -n foo ] # Has value
|
||||||
|
|
||||||
|
# Identical tests to verify that a value is empty
|
||||||
|
[ ! -n foo ] # Not is non-empty
|
||||||
|
[ -z foo ] # Is empty
|
||||||
|
|
||||||
|
### Exceptions:
|
||||||
|
|
||||||
|
This is a stylistic issue that does not affect correctness. If you prefer the original expression, you can't not [[Ignore]] it with a directive or flag.
|
||||||
|
|
||||||
|
### Related resources:
|
||||||
|
|
||||||
|
* Help by adding links to BashFAQ, StackOverflow, man pages, POSIX, etc!
|
Reference in New Issue
Block a user