bash: How do I ensure termination of process substitution used with exec?

前端 未结 5 838
时光说笑
时光说笑 2021-01-06 08:48

If I run

$#/bin/bash
for i in `seq 5`; do
    exec 3> >(sed -e \"s/^/$i: /\"; echo \"$i-\")
    echo foo >&3
    echo bar >&3
    exec 3&         


        
5条回答
  •  一生所求
    2021-01-06 09:17

    Easy, just pipe everything into cat.

    #!/bin/bash
    for i in `seq 5`; do
      {
      exec 3> >(sed -e "s/^/$i: /"; echo "$i-")
      echo foo >&3
      echo bar >&3
      exec 3<&-
      }|cat
    done
    

    Here's the output:

    1: foo
    1: bar
    1-
    2: foo
    2: bar
    2-
    3: foo
    3: bar
    3-
    4: foo
    4: bar
    4-
    5: foo
    5: bar
    5-
    

提交回复
热议问题