coproc help - a new feature in bash

2010-05-10 0 words 0 mins read

Image via Wikipedia

In the <a class="zem_slink freebase/en/bash" title="Bash" rel="homepage" href="">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="">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="">good substitution for <a class="zem_slink freebase/guid/9202a8c04000641f800000000048fac5" title="Pipeline (Unix)" rel="wikipedia" href="">pipes in lots of cases. You can learn more about coproc in the link below:

<a href="">The coproc keyword [Bash Hackers Wiki]

From the <a class="zem_slink freebase/en/manual_page" title="Man page" rel="wikipedia" href="">man page for bash:
A coprocess is a shell command preceded by the coproc <a class="zem_slink freebase/en/reserved_word" title="Reserved word" rel="wikipedia" href="">reserved word. A
coprocess is <a class="zem_slink freebase/en/capital_punishment" title="Capital punishment" rel="wikipedia" href="">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="">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="">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="">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="">thinkering with bash ( <li class="zemanta-article-ul-li"><a href="">bash builtins ( <li class="zemanta-article-ul-li"><a href="">Executing Multiple Commands – A Bash Productivity Tip ( <li class="zemanta-article-ul-li"><a href="">Parallelization in bash ( <li class="zemanta-article-ul-li"><a href="">bash WTFs ( <li class="zemanta-article-ul-li"><a href="">The Shell Game Part 3 ( <div class="zemanta-pixie"><a class="zemanta-pixie-a" title="Reblog this post [with Zemanta]" href=""><img class="zemanta-pixie-img" src="" alt="Reblog this post [with Zemanta]" /><span class="zem-script more-related more-info pretty-attribution paragraph-reblog">


Authored By Amit Agarwal

Amit Agarwal, Linux and Photography are my hobbies.Creative Commons Attribution 4.0 International License.

We notice you're using an adblocker. If you like our webite please keep us running by whitelisting this site in your ad blocker. We’re serving quality, related ads only. Thank you!

I've whitelisted your website.

Not now
This website uses cookies to ensure you get the best experience on our website. Learn more Got it