-
Notifications
You must be signed in to change notification settings - Fork 1.9k
SC2312
Consider invoking this command separately to avoid masking its return value (or use '|| true' to ignore).
This is an optional suggestion enabled with shellcheck -o check-extra-masked-returns or enable=check-extra-masked-returns in a # shellcheck directive or .shellcheckrc.
set -e
cd "$(get_chroot_dir)/etc"
tar xf "${config}"set -e
dir="$(get_chroot_dir)"
cd "${dir}/etc"
tar xf "${config}"set -e
dir="$(get_chroot_dir)"
[[ -d "${dir}" ]] || exit 1
cd "${dir}/etc"
tar xf "${config}"In the problematic example, the exit code for get_chroot_dir is ignored because it is used in a command substitution in the argument of another command.
If the command shows error: Can't determine chroot and exits with failure without outputting a directory, then the command being run will be cd "/etc" and the script will proceed to overwrite the host system's configuration.
By assigning it to a variable first, the exit code of the command will propagate into the exit code of the assignment, so that it can be checked explicitly with if or implicitly with set -e.
If you don't care about the command's exit status, already handle it through a side channel like <(cmd; echo $? > status), or (in the case of background processes and process substitution) wait on the result like <(cmd) ; wait $!, then you can either ignore the suggestion with a directive, or use || true (or || :) to suppress it.
https://mywiki.wooledge.org/BashPitfalls#cmd1_.26.26_cmd2_.7C.7C_cmd3
- Help by adding links to BashFAQ, StackOverflow, man pages, POSIX, etc!