Pattern: Masking command return value
Issue: -
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
.
Example of incorrect code:
set -e
cd "$(get_chroot_dir)/etc"
tar xf "${config}"
Example of correct code:
set -e
dir="$(get_chroot_dir)"
cd "${dir}/etc"
tar xf "${config}"