Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

SC2319 - confusing warning message when checking $? after [[ ]] #2544

Closed
4 tasks done
aslatter opened this issue Jul 24, 2022 · 3 comments
Closed
4 tasks done

SC2319 - confusing warning message when checking $? after [[ ]] #2544

aslatter opened this issue Jul 24, 2022 · 3 comments

Comments

@aslatter
Copy link

For bugs

  • Rule Id (if any, e.g. SC1000): SC2319
  • My shellcheck version (shellcheck --version or "online"): v0.8.0-45-gd1d574c
  • 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 - (not really, but "--version" indicates I am using the current commit)

For new checks and feature suggestions

Here's a snippet or screenshot that shows the problem:

#!/bin/bash

[[ -n "${DB_HOST:-}" && -n "${DB_TOKEN:-}" && -n "${DISTRO:-}" ]]
should_check_db=$?

echo "$should_check_db"

Here's what shellcheck currently says:

In /work/test.sh line 4:
should_check_db=$?
                ^-- SC2319 (warning): This $? refers to a condition, not a command. Assign to a variable to avoid it being overwritten.

For more information:
  https://www.shellcheck.net/wiki/SC2319 -- This $? refers to a condition, no...

Here's what I wanted or expected to see:

It's confusing that shellcheck is telling me to assign $? to a variable on the line where I assign $? to a variable.

It's not clear why I shouldn't be checking the exit-code of "[[]]" - that's the whole point of "[[]]". None of the "problematic code" examples in the wiki look like the code I have, also.

Better descriptions or examples around why this construction is problematic would be helpful.

@koalaman
Copy link
Owner

Agreed, your pattern was listed under "Exceptions". However, the check has now been updated to exclude cases where the $? is used unconditionally (like your case or [ -d test ]; return $?). Thanks for testing on the git branch!

@aslatter
Copy link
Author

Thanks for the fix! The change works great.

@mgmsam
Copy link

mgmsam commented Sep 10, 2022

Hi all!!

Conducted a test on the site https://www.shellcheck.net
I report that this problem is repeated inside a function:

Here's a snippet or screenshot that shows the problem:

#!/bin/bash
print_codout() {
	RETURN=$?
	printf '%s\n' "$RETURN"
}
[ -f foo ] || print_codout

Here's what shellcheck currently says:

$ shellcheck myscript

Line 3
RETURN=$?
       ^-- SC2319 (warning): This $? refers to a condition, not a command. Assign to a variable to avoid it being overwritten.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants