Settings

Theme

Coproc Tutorial (2011)

zsh.org

26 points by 0x45696e6172 2 years ago · 7 comments

Reader

o11c 2 years ago

I've never found a case where coproc actually solves a problem I'm facing.

Note that bash's version is slightly different but pretty close. Also, bash supports `lastpipe` nowadays so you don't have to use this as a workaround for that issue, and `< <()` is more often the useful thing for custom pipeline composability, and without suffering from the "only one coprocess may exist" limitation.

  • cryptonector 2 years ago

    You can get the pid of the process in the process redirection case (`while read ...; do ...; done < <(some_program)`) but not in the | case (`some_program | while read ...; do ...; done`).

  • nerdponx 2 years ago

    I used it once for configuring Herbstluftwm and Dzen, but it was very fussy and I ended up refactoring my script later into separate processes.

  • 0x45696e6172OP 2 years ago

    I use it to launch GUI apps from the terminal, e.g.

        coproc 'nemo .'
    • wiml 2 years ago

      Is that better than using ordinary job control (nemo . &) ?

      • cryptonector 2 years ago

        Possibly. There are race conditions with `wait` for `$somepid` because shells kinda have to reap processes as soon as possible, but that means that your `$somepid` can be stale and even refer to some other process. If you have a pipe from the process' stdout and/or stderr then when you can try to read from that pipe to know if the process exited -- you might still lose the exit status, unless you run the program like `(the_program ...; stat=$?; printf '%s\n' $stat 1>&2; exit $stat) &` and then read the exit status from the stderr pipe.

      • wavemode 2 years ago

        Most GUI apps are noisy as hell in their output.

        Though I guess there's always nemo . >/dev/null 2>&1 &

Keyboard Shortcuts

j
Next item
k
Previous item
o / Enter
Open selected item
?
Show this help
Esc
Close modal / clear selection