0
#!/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?