Skip to content

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

@macalixx

Description

@macalixx

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions