shellcheck icon indicating copy to clipboard operation
shellcheck copied to clipboard

SC2319 - changed with Shellcheck 0.10.0

Open escoand opened this issue 1 year ago • 7 comments

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 --version or "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:

image

#!/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

escoand avatar Mar 08 '24 12:03 escoand

Same problem with VS Code add-on (https://marketplace.visualstudio.com/items/timonwong.shellcheck/changelog) - it uses 0.10.0:
image

For https://www.shellcheck.net/ all good (maybe due to not updated):
image

Stikus avatar Mar 12 '24 13:03 Stikus

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.

cbf123 avatar Mar 19 '24 20:03 cbf123

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.

Stikus avatar Apr 03 '24 10:04 Stikus

Same here! Hope to see the fix... Help, please!

serge2016 avatar Jun 07 '24 08:06 serge2016

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
}

bbrk24 avatar Oct 02 '24 22:10 bbrk24

Still hope for a fix.

serge2016 avatar Oct 03 '24 05:10 serge2016

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.

fwdekker avatar Feb 15 '25 12:02 fwdekker