As far as I understand, the following bash-script should be aborting after the second statement, because the command meant to initialize the variable a
fails:
set -eu -o pipefail
read a <<< $(false)
echo Hi there: '>'$a'<'
Instead the variable is assigned an empty string and the script proceeds to print: Hi there: ><
.
How can I reliably -- distinguishing actual failures from empty output -- detect the failure of the command used for such "here-string" assignments?
That's simple, store the string.
set -euo pipefail
tmp=$(false)
read -r a <<<"$tmp"
echo Hi there: '>'"$a"'<'
Normally, the exit status of an assignment is 0, unless there is one or more command substitutions in the right-hand side, in which case the exit status is the exit status of the last command substitution.
The exit status of the command substitution doesn't affect the exit status of read
, though, when it appears in a here string.