In Bash, how can I compute and capture the result from an intermediate node in a pipeline?
For example, given a list of integers and a command or function maximum, using a single Bash pipeline, how can I capture the maximum value of the list of integers in a variable, but also output the list to standard output?
In the following Bash script, the idea is to change function capture_maximum_and_print_numbers so that it is a single pipeline.
function maximum {
awk 'NR==1||$0>x{x=$0}END{print x}'
}
function capture_maximum_and_print_numbers {
local numbers="3\n6\n1\n4\n2\n9\n5\n"
maximum=$(printf $numbers | maximum)
printf $numbers
}
capture_maximum_and_print_numbers
printf "maximum=$maximum\n"