On Sun, 27 Jun 2021, at 01:13, Bruno Haible wrote:
> So, people come to recommend the 'sponge' program from moreutils [2]:
>
> prog1 < FILE | prog2 | prog3 | sponge FILE
>
> But this program has the drawback that, requiring another step in the
> pipeline, the exit code of the previous command gets lost.
To this specific point, there are bash options (not sure about other shells)
Option 1:
bash provides PIPESTATUS as an array of exit codes.
$ seq 50 | grep apple | wc -l
0
$ echo ${PIPESTATUS[*]}
0 1 0
Option 2:
bash provides the "pipefail" to expose a non-zero exit status from mid-pipeline.
$ set -o pipefail
$ seq 50 | grep apple | wc -l
0
$ echo $?
1
Cheers,
Phil