Skip to content
wileyhy edited this page Oct 6, 2024 · 5 revisions

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.

Problematic code:

set -e
cd "$(get_chroot_dir)/etc"
tar xf "${config}"

Correct code:

set -e
dir="$(get_chroot_dir)"
cd "${dir}/etc"
tar xf "${config}"

Correct code: (with correction)

set -e
dir="$(get_chroot_dir)"
[[ -d "${dir}" ]] || exit 1
cd "${dir}/etc"
tar xf "${config}"

Rationale:

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.

Exceptions:

If you don't care about the command's exit status, or already handle it through a side channel like <(cmd; echo $? > status), then you can either ignore the suggestion with a directive, or use || true (or || :) to suppress it.

Related resources:

https://mywiki.wooledge.org/BashPitfalls#cmd1_.26.26_cmd2_.7C.7C_cmd3

  • Help by adding links to BashFAQ, StackOverflow, man pages, POSIX, etc!

ShellCheck

Each individual ShellCheck warning has its own wiki page like SC1000. Use GitHub Wiki's "Pages" feature above to find a specific one, or see Checks.

Clone this wiki locally