Skip to content

SC2320: false negative for printf #3490

Description

@e-kwsm

For bugs

  • Rule Id (if any, e.g. SC1000): SC2320
  • My shellcheck version (shellcheck --version or 'online'): online
  • I tried on shellcheck.net and verified that this is still a problem on the latest commit
  • It's not reproducible on shellcheck.net, but I think that's because it's an OS, configuration or encoding issue

For new checks and feature suggestions

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

#!/bin/sh
mycommand
# echo "Command exited with $?"       # OK, SC2320 detected
printf 'Command exited with %d\n' $?  # SC2320 NOT detected
if [ $? -ne 0 ]
then
  echo "Failed"
fi

Here's what shellcheck currently says:

Line 5	SC2181: Check exit code directly with e.g. 'if ! mycmd;', not indirectly with $?.

Here's what I wanted or expected to see:

+Line 5	SC2320: This $? refers to echo/printf, not a previous command. Assign to variable to avoid it being overwritten.
 Line 5	SC2181: Check exit code directly with e.g. 'if ! mycmd;', not indirectly with $?.

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