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

SC2031 missing issues with process substitution (<()) #3084

Open
4 tasks
stdedos opened this issue Nov 14, 2024 · 1 comment
Open
4 tasks

SC2031 missing issues with process substitution (<()) #3084

stdedos opened this issue Nov 14, 2024 · 1 comment

Comments

@stdedos
Copy link
Contributor

stdedos commented Nov 14, 2024

For bugs

For new checks and feature suggestions

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

#!/bin/bash

set -Eeuo pipefail

random_fail() {
  if [ $((RANDOM % 100)) -lt 50 ]; then
    return 1
  fi
  return 0
}

random_fail | tee /tmp/a

# I don't want this to fail immediately, but I want the exit code
# ... also I have other pipes I want to guard against (instead of "consuming" the $?)

(random_fail && RC=0 || RC=$?) | tee /tmp/a
echo "RC: $RC"

# But that triggers SC2030/SC2031. There's a suggestion about using <(), so ... might work?

tee /tmp/a < <(random_fail && RC=0 || RC=$?)
echo "RC: $RC"

# Fun fact: It won't

Here's what shellcheck currently says:

[Line 17:](javascript:setPosition(17, 17))
(random_fail && RC=0 || RC=$?) | tee /tmp/a
                ^-- [SC2030](https://www.shellcheck.net/wiki/SC2030) (info): Modification of RC is local (to subshell caused by (..) group).
 
[Line 18:](javascript:setPosition(18, 11))
echo "RC: $RC"
          ^-- [SC2031](https://www.shellcheck.net/wiki/SC2031) (info): RC was modified in a subshell. That change might be lost.

Here's what I wanted or expected to see:

+ Also for l22, l23

@stdedos
Copy link
Contributor Author

stdedos commented Nov 14, 2024

Solution for the ones who are searching:

if random_fail | tee /tmp/a ; then
  RC=0
else
  RC=$?
fi

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

1 participant