What is `coproc` and when would you use it?
AdvancedConceptArguments, I/O, here-docs, process substitution and subshells
Answer
coproc starts a background process with two-way pipes: you write to its stdin and read its stdout via file descriptors in an array.
coproc BC { bc -l; }
echo "scale=4; 22/7" >&"${BC[1]}"
read -r result <&"${BC[0]}"
echo "$result" # 3.1428
It is rare in DevOps scripts; it suits long-lived helpers (a bc calculator, a persistent psql session) where starting a new process per query is too slow. If you need this often, the task has probably outgrown Bash.
More on Arguments, I/O, here-docs, process substitution and subshells
How does `getopts` work? Show a complete example.IntermediateWhat is the difference between `getopts` and `getopt`?AdvancedExplain file descriptors 0, 1 and 2 and the main redirection operators.BeginnerWhy is `cmd 2>&1 > file` different from `cmd > file 2>&1`?AdvancedWhat is a here-document, and what is the difference between `<<EOF`, `<<'EOF'` and `<<-EOF`?BeginnerWhat is a here-string (`<<<`)?Intermediate