Inform right-hand side of pipeline of left-side failure? Inform right-hand side of pipeline of left-side failure? bash bash

Inform right-hand side of pipeline of left-side failure?


Use set -o pipefail on top of your bash script so that when the left side of the pipe fails (exit status != 0), the right side does not execute.


Does the pipe process continue even if the first process has ended, or is the issue that you have no way of knowing that the first process failed?

If it's the latter, you can look at the PIPESTATUS variable (which is actually a BASH array). That will give you the exit code of the first command:

parse_commands /da/cmd/file | process_commandstemp=("${PIPESTATUS[@]}")if [ ${temp[0]} -ne 0 ]then    echo 'parse_commands failed'elif [ ${temp[1]} -ne 0 ]then    echo 'parse_commands worked, but process_commands failed'fi

Otherwise, you'll have to use co-processes.


Unlike the and operator (&&), the pipe operator (|) works by spawning both processes simultaneously, so the first process can pipe its output to the second process without the need of buffering the intermediate data. This allows for processing of large amounts of data with little memory or disk usage.

Therefore, the exit status of the first process wouldn't be available to the second one until it's finished.