0

How to put the bash subshell sed output into the main/root shell environment variable ?

it'll fail,

v=`echo NO |tee >(sed 's/^/&YES/' )`

How's the true to solve it?

1 Answers1

2

It works (Linux Mint 18.1, GNU bash, version 4.3.48).

Paul--) unset v
Paul--) v=`echo NO |tee >(sed 's/^/&YES/' )`
Paul--) declare -p v
declare -- v="NO
YESNO"

What did you expect it to do? The issue is that you are using >( .. ), a Process Substitution syntax.

tee makes a copy of the NO and pipes it through sed. So the tee outputs the original NO\n to its stdout, and the sed runs as a separate process and sends NOYES\n to its stdout. The command substitution (in the back-quotes) collects both into v.

The sed runs in a separate process, and there have been issues in the past with bash not waiting for it to complete. So there is the possibility of a race condition with this construct, especially if the second process takes some time.

Paul_Pedant
  • 8,679