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

SC2116 (Useless echo?) and SC2046 (prevent word splitting) should not apply if in case of SC2327 (command substitution will be empty). #3043

Open
2 of 4 tasks
mk-pmb opened this issue Aug 21, 2024 · 2 comments

Comments

@mk-pmb
Copy link

mk-pmb commented Aug 21, 2024

For bugs

  • Rule Id (if any, e.g. SC1000): SC2046, SC2116, SC2327, SC2328
  • My shellcheck version (shellcheck --version or "online"): online
  • The rule's wiki page does not already cover this (e.g. https://shellcheck.net/wiki/SC2086)
  • I tried on https://www.shellcheck.net/ and verified that this is still a problem on the latest commit

For new checks and feature suggestions

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

#!/bin/bash
python "$PY" "$@" || return $?$(
  echo E: "Python script '$PY' with $# arguments failed, rv=$?" >&2)

Here's what shellcheck currently says:

Line 2:
python "$PY" "$@" || return $?$(
                              ^-- [SC2046](https://www.shellcheck.net/wiki/SC2046) (warning): Quote this to prevent word splitting.
                              ^-- [SC2327](https://www.shellcheck.net/wiki/SC2327) (warning): This command substitution will be empty because the command's output gets redirected away.
                              ^-- [SC2116](https://www.shellcheck.net/wiki/SC2116) (style): Useless echo? Instead of 'cmd $(echo foo)', just use 'cmd foo'.
 
Line 3:
  echo E: "Python script '$PY' with $# arguments failed, rv=$?" >&2)
>>                                                              ^-- [SC2328](https://www.shellcheck.net/wiki/SC2328) (error): This redirection takes output away from the command substitution (use tee to duplicate).

Here's what I wanted or expected to see:

When the command substitution will be empty (SC2327), I expect to not be warned about word-splitting of the empty result (SC2046).

When the reason why the substitution will be empty is that all of it will be redirected away (SC2327), the echo shall not be considered useless (SC2116).

I want an option to disable SC2328 ("This redirection takes output away from the command substitution (use tee to duplicate).") only in cases where the substitution is guaranteed to be empty. Even better if I could further restrict it to being used in a "return" or "exit" instruction.

@plambert
Copy link

This is interesting; I've never seen process substitution used this way, to effectively create a compound statement without affecting $?. Typically, I've done this:

#!/bin/bash
python "$PY" "$@" || { rc=$?; echo E: "Python script '${PY}' with $# arguments failed, rv=${rc}" >&2; return $rc; }

Personally, I prefer my method, but that certainly doesn't make it right. 😉

I'd support this change, but unfortunately, I don't have the skills to attempt to write a PR.

@mk-pmb
Copy link
Author

mk-pmb commented Aug 22, 2024

The advantage of my way is that you don't have to pick a name for a temporary variable, and it's shorter overall. Also I'm not sure how portable it is to use local within { … }; if not, you'd have to pre-declare your temporary variable to not pollute a caller's namespace.

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