set -e doesn't trigger when command substitutions returns, why?
From:
Никола Константинов Кралев
Subject:
set -e doesn't trigger when command substitutions returns, why?
Date:
Wed, 18 Jun 2025 04:57:01 +0300 (EEST)
Configuration Information [Automatically generated, do not change]:
Machine: x86_64
OS: linux-gnu
Compiler: gcc
Compilation CFLAGS: -g -O2 -fdebug-prefix-map=/build/bash-Smvct5/bash-5.0=.
-fstack-protector-strong -Wformat -Werror=format-security -Wall
-Wno-parentheses -Wno-format-security
uname output: Linux user1-VirtualBox 5.15.0-139-generic #149~20.04.1-Ubuntu SMP
Wed Apr 16 08:29:56 UTC 2025 x86_64 x86_64 x86_64 GNU/Linux
Machine Type: x86_64-pc-linux-gnuBash Version: 5.0
Patch Level: 17
Release Status: releaseDescription:
[Detailed description of the problem, suggestion, or complaint.]
When we use set -e, or pass -e to bash itself, -e does process status code
checks as written in the bash man page.With some testing I think I found a few
corner cases which I am not sure if they are bugs or not.
I have added some demonstrations below in Repeat-By below.In test1, we see that
despite the ls command failing, we see print1 printed, so the error checking of
set -e was ignored?
In test2 we see that when commands are grouped in parenthesis, set -e works
correctly and we never see print1 shown
In test3, we see that the $? value is treated as whatever /bin/echo '' does,
and that's a success code 0, and the failure of ls was completely
ignored.Repeat-By:
[Describe the sequence of events that causes the problem
to occur.]#test1
/bin/bash -c 'set -e ; /bin/echo $(ls /doesnt_exist) ; echo print1'
#and the stdout/stderr displayed is:
ls: cannot access '/doesnt_exist': No such file or directoryprint1
#test2
bin/bash -c 'set -e ; (ls /doesnt_exist) ; echo print1'
#and the stdout/stderr displayed is:
ls: cannot access '/doesnt_exist': No such file or directory
#test3
/bin/bash -c 'set -e ; /bin/echo $(ls /doesnt_exist) ; echo $?'
#and the stdout/stderr displayed is:
ls: cannot access '/doesnt_exist': No such file or directory0
Fix:
[Description of how to fix the problem. If you don't know a
fix for the problem, don't include this section.]Change set -e to track errors
from test1 and test3 maybe?