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:

4
  • process="$(false | true ; echo ${PIPESTATUS[@]} >&2)" or process="$(false | true ; echo ${PIPESTATUS[@]} >/dev/tty)" Commented Nov 16, 2019 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. Commented Nov 16, 2019 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 Commented Nov 16, 2019 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). Commented Nov 16, 2019 at 17:26

0

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.