coproc help - a new feature in bash

2010-05-10 0 min read bash Fedora Linux
\"Screenshot
Image via Wikipedia

In the <a class="zem_slink freebase/en/bash" title="Bash" rel="homepage" href="http://tiswww.case.edu/php/chet/bash/bashtop.html">bash version 4.0, there is a new concept called coproc. This is very useful for some of the daily tasks.

co-<a class="zem_slink freebase/guid/9202a8c04000641f800000000005a409" title="Process (computing)" rel="wikipedia" href="http://en.wikipedia.org/wiki/Process_%28computing%29">process starts a process in the background, optionally with a NAME, with which other processes can communicate. This can be a very <a class="zem_slink freebase/en/substitute_good" title="Substitute good" rel="wikipedia" href="http://en.wikipedia.org/wiki/Substitute_good">good substitution for <a class="zem_slink freebase/guid/9202a8c04000641f800000000048fac5" title="Pipeline (Unix)" rel="wikipedia" href="http://en.wikipedia.org/wiki/Pipeline_%28Unix%29">pipes in lots of cases. You can learn more about coproc in the link below:

<a href="http://bash-hackers.org/wiki/doku.php/syntax/keywords/coproc">The coproc keyword [Bash Hackers Wiki]

From the <a class="zem_slink freebase/en/manual_page" title="Man page" rel="wikipedia" href="http://en.wikipedia.org/wiki/Man_page">man page for bash:
Coprocesses
A coprocess is a shell command preceded by the coproc <a class="zem_slink freebase/en/reserved_word" title="Reserved word" rel="wikipedia" href="http://en.wikipedia.org/wiki/Reserved_word">reserved word. A
coprocess is <a class="zem_slink freebase/en/capital_punishment" title="Capital punishment" rel="wikipedia" href="http://en.wikipedia.org/wiki/Capital_punishment">executed asynchronously in a subshell, as if the command
had been terminated with the & <a class="zem_slink freebase/en/control_flow" title="Control flow" rel="wikipedia" href="http://en.wikipedia.org/wiki/Control_flow">control operator, with a two-way pipe
established between the executing shell and the coprocess.

The format for a coprocess is:

coproc [NAME] command [redirections]

This creates a coprocess named NAME. If NAME is not supplied, the
default name is COPROC. NAME must not be supplied if command is a sim-
ple command (see above); otherwise, it is interpreted as the first word
of the simple command. When the coproc is executed, the shell creates
an array variable (see Arrays below) named NAME in the context of the
executing shell. The <a class="zem_slink freebase/en/standard_streams" title="Standard streams" rel="wikipedia" href="http://en.wikipedia.org/wiki/Standard_streams">standard output of command is connected via a
pipe to a <a class="zem_slink freebase/en/file_descriptor" title="File descriptor" rel="wikipedia" href="http://en.wikipedia.org/wiki/File_descriptor">file descriptor in the executing shell, and that file
descriptor is assigned to NAME[0]. The standard input of command is
connected via a pipe to a file descriptor in the executing shell, and
that file descriptor is assigned to NAME[1]. This pipe is established
before any redirections specified by the command (see REDIRECTION
below). The file descriptors can be utilized as arguments to shell
commands and redirections using standard word expansions. The process
id of the shell spawned to execute the coprocess is available as the
value of the variable NAME_PID. The wait builtin command may be used
to wait for the coprocess to terminate.<h6 class="zemanta-related-title">Related articles by Zemanta <ul class="zemanta-article-ul"> <li class="zemanta-article-ul-li"><a href="http://almirkaric.com/2010/5/2/thinkering-with-bash/">thinkering with bash (almirkaric.com) <li class="zemanta-article-ul-li"><a href="http://almirkaric.com/2010/5/2/bash-builtins/">bash builtins (almirkaric.com) <li class="zemanta-article-ul-li"><a href="http://www.skorks.com/2010/05/executing-multiple-commands-a-bash-productivity-tip/">Executing Multiple Commands – A Bash Productivity Tip (skorks.com) <li class="zemanta-article-ul-li"><a href="http://almirkaric.com/2010/5/2/parallelization-in-bash/">Parallelization in bash (almirkaric.com) <li class="zemanta-article-ul-li"><a href="http://almirkaric.com/2010/5/2/bash-wtfs/">bash WTFs (almirkaric.com) <li class="zemanta-article-ul-li"><a href="http://www.slideshare.net/Ahuka/the-shell-game-part-3">The Shell Game Part 3 (slideshare.net) <div class="zemanta-pixie"><a class="zemanta-pixie-a" title="Reblog this post [with Zemanta]" href="http://reblog.zemanta.com/zemified/1823ef0d-c68e-4c1f-96a9-0624fa82db3b/"><img class="zemanta-pixie-img" src="http://blog.amit-agarwal.co.in/wp-content/uploads/2010/08/reblog_b75.png" alt="Reblog this post [with Zemanta]" /><span class="zem-script more-related more-info pretty-attribution paragraph-reblog">

comments powered by Disqus