shellcheck
shellcheck copied to clipboard
Avoid multiple substitutions in a single statement
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.
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[@]}"