bashio-redirectionprocess-substitution

Order of redirection and process substitution


What is the process order of redirection and process substitution? One line command to process pre error works Ok.

{ echo hello;jiji jio; } 2> >(while read line;do echo "____$line";done) >right.log

While this order does not.

{ echo hello;jiji jio; } >right.log 2> >(while read line;do echo "____$line";done)

Solution

  • $ { echo hello;jiji jio; } 2> >(while read line;do echo "____$line";done) >right.log
    ____jiji: command not found
    $ cat right.log 
    hello
    

    In the above, it is like jiji jio 2>&1 | while read line;do echo "____$line";done and echo hello >right.log.

    $ { echo hello;jiji jio; } >right.log 2> >(while read line;do echo "____$line";done)
    $ cat right.log 
    hello
    ____jiji: command not found
    
    

    In the second, it is like { echo hello; jiji jio 2>&1 | while read line;do echo "____$line";done } >right.log. Remember that echo "____$line" portion triggers it to be redirected to >right.log (because it writes to stdout).

    So yes, order does matter. In the second case >right.log redirection is remembered when processing 2> >(...) and whenever ... writes to stdout it is redirected to >right.log.