#!/usr/bin/env bash
set -euxo pipefail
echo $(echo "$THIS_VAR_DOES_NOT_EXIST" | cat)
echo "WTF"
Running this script produces:
./wtf.sh
++ cat
./wtf.sh: line 4: THIS_VAR_DOES_NOT_EXIST: unbound variable
+ echo
+ echo WTF
WTF
Why does it run the last command echo WTF? I assume it would terminate the script at line 4 (due to the presence of set -e).
Does subshells not automatically inherit the set -e option? Or is this some special behaviour?
On the other hand if it seems if I don't echo the command and instead output it to a variable it behaves as expected... What?????
#!/usr/bin/env bash
set -euxo pipefail
echo $(echo "$THIS_VAR_DOES_NOT_EXIST")
echo "this works fine..."
yolo=$(echo "$THIS_VAR_DOES_NOT_EXIST")
echo "this does not"
echo "..."
Produces:
./wtf.sh: line 4: THIS_VAR_DOES_NOT_EXIST: unbound variable
+ echo
+ echo 'this works fine...'
this works fine...
./wtf.sh: line 7: THIS_VAR_DOES_NOT_EXIST: unbound variable
+ yolo=
Why does this behave differently?
set -u). Exiting a subshell will not exit the main shell though.echo $(exit); echo foo,exit | cat; echo foo, etc will all go on and echofoo. – Mar 20 '20 at 13:27var=$(...)intermediate variable assignment is just the way out of this, because the exit status of such an expression will be that of the command substitution$(...). Inecho $(...)the exit status of the command will be that ofecho, ie. 0. Try witha=$(exit 13); echo $?vsecho $(exit 13); echo $?– Mar 20 '20 at 14:34