shellcheck icon indicating copy to clipboard operation
shellcheck copied to clipboard

Avoid multiple substitutions in a single statement

Open Atry opened this issue 3 years ago • 1 comments
trafficstars

For new checks and feature suggestions

  • [x] https://www.shellcheck.net/ (i.e. the latest commit) currently gives no useful warnings about this
  • [x] I searched through https://github.com/koalaman/shellcheck/issues and didn't find anything related

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

#!/usr/bin/bash
set -e
ECHO_FLAGS=("$(echo Failed to produce a flag >&2; false)" "$(true)" I am executed unexpectedly)
echo "${ECHO_FLAGS[@]}"

Here's what shellcheck currently says:

Nothing

Here's what I wanted or expected to see:

Avoid multiple substitutions in a single statement when the option `set -e` is used, because all the exit codes other than the last substitution would be discarded silently.

Atry avatar Mar 17 '22 20:03 Atry

Ideally += should be used to ensure the exit codes of all substitutions are not discarded.

set -e
echo_flags+=("$(echo Failed to produce a flag >&2;  false)")
echo_flags+=("$(true)")
echo_flags+=(I am executed unexpectedly)
echo "${echo_flags[@]}"

Atry avatar Mar 17 '22 21:03 Atry