0

A classical example of piping:

$ false | true
$ echo ${PIPESTATUS[@]}
1 0

Same example run in a subshell and assigning results into a variable:

$ process="$(false | true)"
$ echo ${PIPESTATUS[@]}
0

How could I get the exit status of each of the piped processes?

Similar question, without the subshell part:

  • process="$(false | true ; echo ${PIPESTATUS[@]} >&2)" or process="$(false | true ; echo ${PIPESTATUS[@]} >/dev/tty)" – Jesse_b Nov 16 at 16:55
  • well, you do the echo ${PIPESTATUS} inside the command substitution subshell ;-) I guess your question is more about how to capture output other than stdout from a command substitution. – mosvy Nov 16 at 16:56
  • ... which is "interesting", since there are no nice solutions, only ugly kludges. For instance, this will use a tempfile to get the data out, while leaving stdout and stderr undisturbed: t=$(mktemp); exec 3>$t 4<$t; rm "$t"; out=$(false | true | false | echo hmm; echo -n "${PIPESTATUS[@]}" >&3); readarray -td' ' st <&4; exec 3>&- 4<&-; echo "out=$out; st=(${st[*]})". Related: Redirect STDERR and STDOUT to different variables without temporary files – mosvy Nov 16 at 17:22
  • btw, you got you quoting backwards: you should quote the ${PIPESTATUS[@]} in echo "${PIPESTATUS[@]}", but you don't have to quote the $(...) in process=$(false | true). – mosvy Nov 16 at 17:26

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.