Personal shell script guide.
You may want to replace a simple if
block with a one-liner.
func_intended() {
# ...
if [ -z "$SOME_VAR" ]; then
printf "ERROR\n"
return 1
fi
return # info: $? is 0
}
func_problematic() {
# ...
[ -z "$SOME_VAR" ] && { printf "ERROR\n"; return 1; }
return # warning: $? is 1
}
func_correct() {
# ...
[ -n "$SOME_VAR" ] || { printf "ERROR\n"; return 1; }
return # info: $? is 0
}
It seems the current version of ShellCheck fails to find this kind of programming errors at the time of writing.
It detects an issue in code like A && B || C
but it is a different type of problem.
See ShellScheck SC2015 regarding A && B || C
.
See wiki for code snippets.
ShellCheck is a great tool.