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 - confused warning message when checking $? inside a function #2571

Open
mgmsam opened this issue Sep 10, 2022 · 4 comments
Open

SC2319 - confused warning message when checking $? inside a function #2571

mgmsam opened this issue Sep 10, 2022 · 4 comments

Comments

@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.

Originally posted by @mymegasam in #2544 (comment)

@ThanAngell
Copy link

ThanAngell commented Mar 21, 2023

I concur that this appears to be a incorrect warning. I received the same warning in a function being called from an exit trap.

cleanup()
{
  rc=$?
  # do cleanup
  return $rc
}

trap 'cleanup;' exit

running locally with version: 0.9.0

@yozachar
Copy link

Happens here too:

#!/bin/bash

if [[ $exit_code -ne 0 ]]; then
    :
else
    exit_code=$?    
fi
Line 25:
    exit_code=$?    
              ^-- [SC2319](https://www.shellcheck.net/wiki/SC2319) (warning): This $? refers to a condition, not a command. Assign to a variable to avoid it being overwritten.

@mgmsam mgmsam closed this as completed Sep 3, 2023
@Hi-Angel
Copy link

Hi-Angel commented Sep 3, 2023

@mgsmgit was it fixed? I don't seem to see references from commits to this issue…

@mgmsam
Copy link
Author

mgmsam commented Sep 3, 2023

@mgsmgit was it fixed? I don't seem to see references from commits to this issue…
Oh, it's a mistake, I accidentally closed it, I'm sorry.

@mgmsam mgmsam reopened this Sep 3, 2023
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

4 participants