shellcheck
shellcheck copied to clipboard
SC2319 - changed with Shellcheck 0.10.0
Shellcheck in 0.10.0 complains about an error which is explicit mentioned in the wiki as not relevant. Shellcheck 0.9.0 and also shellcheck.net don't warn. But this is just happening in some cases, see below.
For bugs
- Rule Id (if any, e.g. SC1000): SC2319
- My shellcheck version (
shellcheck --versionor "online"): 0.10.0 - [x] The rule's wiki page does not already cover this (e.g. https://shellcheck.net/wiki/SC2086)
- [ ] I tried on https://www.shellcheck.net/ and verified that this is still a problem on the latest commit
For new checks and feature suggestions
- [x] https://www.shellcheck.net/ (i.e. the latest commit) currently gives no useful warnings about this
- [ ] I searched through https://github.com/koalaman/shellcheck/issues and didn't find anything related
Here's a snippet or screenshot that shows the problem:
#!/bin/sh
test -z /test
echo $?
foo=$(echo)
export foo
test -z /test
echo $?
Here's what shellcheck currently says:
In test.sh line 4:
echo $?
^-- SC2319 (warning): This $? refers to a condition, not a command. Assign to a variable to avoid it being overwritten.
Here's what I wanted or expected to see:
nothing
Same problem with VS Code add-on (https://marketplace.visualstudio.com/items/timonwong.shellcheck/changelog) - it uses 0.10.0:
For https://www.shellcheck.net/ all good (maybe due to not updated):
Just ran into an instance of shellcheck 0.10.0 complaining throwing an SC2319 warning about this code:
#!/bin/bash
[ -f /var/run/.network_upgrade_bootstrap ]
upgr_bootstrap=$?
if [ ${upgr_bootstrap} -eq 0 ]; then
log_it info "Upgrade bootstrap is in execution"
fi
ifaces=$(update_interfaces ${upgr_bootstrap})
update_routes "${ifaces}"
https://www.shellcheck.net/ thinks it's fine. shellcheck-py-0.9.0.6 also thinks it's fine.
Looks like https://github.com/koalaman/shellcheck/issues/2952 is duplicate of this issue.
@koalaman @brother Can anyone take a look? Problem is still here.
Same here! Hope to see the fix... Help, please!
I also ran into this warning where it was unexpected, but in a different way to the original post:
test ! -s "$errors" || {
exit_code=$?
cat "$errors" >&2
return $exit_code
}
Still hope for a fix.
The issue seems to occur when the assignment and subsequent usage of $? are interrupted by a statement that alters $?.
For example, the following gives the warning SC2319 on line 2:
test "$1" != "$2"
foo="$?"
bar="$(cat "bar.txt")"
echo "$foo" "$bar"
but the following does not:
test "$1" != "$2"
foo="$?"
# bar="$(cat "bar.txt")"
echo "$foo" "$bar"
even though in both cases, the value of $? is not statically predictable (because $1 and $2 are unknown) and the output is properly fixed in $foo.