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

SC2320 (warning): This $? refers to echo/printf, not a previous command. #3083

Open
4 tasks done
macalixx opened this issue Nov 13, 2024 · 1 comment
Open
4 tasks done

Comments

@macalixx
Copy link

macalixx commented Nov 13, 2024

For bugs

For new checks and feature suggestions

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

#!/bin/bash

echo "hola" >> /tmp/prueba
Codigo=$?

echo $Codigo

Here's what shellcheck currently says:

Codigo=$?
       ^-- [SC2320](https://www.shellcheck.net/wiki/SC2320) (warning): This $? refers to echo/printf, not a previous command. Assign to variable to avoid it being overwritten.

Here's what I wanted or expected to see:

An "echo" redirected to a file is a file write operation. It is necessary to obtain the exit code of that operation, because writing to a file will fail due to lack of permissions, and sometimes it is necessary to verify it.

Wiki says:

If you intentionally refer to echo to get the result of a write, you can ignore this message. Alternatively, write it out as in if echo $$ > "$pidfile"; then status=0; else status=1; fi

But, I think that a report should be as clean as possible, and I think it is easy to detect these cases so as not to show warnings.

@brother
Copy link
Collaborator

brother commented Nov 13, 2024

Intention is very hard to catch.
I would assume that this is a common error in the wild and that's the reason for having the warning to begin with. And if found it is easy to ignore with a directive as suggested in the wiki.

A patch for making the discovery more robust and to avoid some of these fasle positives is as always welcome.

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

2 participants