bash: coproc

sometimes you need to run a function or whatever in background. One possibility is to use bg but this isn’t very elegant and you have often problems.A better way is to use coproc. Syntax:

coproc [optional NAME] command [optional redirects]

The killing feature  is the name. Finally no %1 and so on. The command is bit strange to code: you enclose the command with curly brackets and end the command with ; (important!).
Here a coding example:

coproc mycoproc { for (( i=0; $i<4; i++ ));do sleep 3; echo $i; done; } #running command
echo "I'm not blocked"
cat <&${mycoproc} #get the results

If you don’t want the “[1]+ finished” you can use head or sed to cut it out.



Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s