1

For a specific shell script, I need a functionality like in this simplified example:

{ replies=`echo output > >(cat >&3; echo reply1) 2> >(cat >&2; echo reply2)` ;} 3>&1

Obivously, I would like $replies to be something like reply1 reply2, but the "second" stream never gets into the variable but comes out at STDOUT:

output
reply2
$ echo $replies 
reply1

It this a bug or a restriction, and how can I work around it?

1 Answer 1

2

Swapping the order of redirections fixes the "problem":

$ { replies=$(echo output  2> >(cat >&2; echo reply2) > >(cat >&3; echo reply1)); } 3>&1
output
$ echo $replies
reply1 reply2

The "problem" in your example is that when cat >&2; echo reply2 is executed the standard output is already redirected to >(cat >&3; echo reply1). As a result the output of echo reply2 goes to cat >&3.

Sign up to request clarification or add additional context in comments.

1 Comment

I actually thought that I tested that some time ago and that it did not work ... but obviously I'm wrong, it works fine for me. Thank you also for the explanation, I even understand now why this is so.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.