A number of things here:
(1) When you say eval $cmd
and attempt to get the exit values of the processes in the pipeline contained in the command $cmd
, echo "${PIPESTATUS[@]}"
would contain only the exit status for eval
. Instead of eval
, you'd need to supply the complete command line.
(2) You need to get the PIPESTATUS
while assigning the output of the pipeline to the variable. Attempting to do that later wouldn't work.
As an example, you can say:
foo=$(command | grep something | command2; echo "${PIPESTATUS[@]})"
This captures the output of the pipeline and the PIPESTATUS
array into the variable foo
.
You could get the command output into an array by saying:
result=($(head -n -1 <<< "$foo"))
and the PIPESTATUS
array by saying
tail -1 <<< "$foo"