I've the following sample script:#!/usr/bin/env bashsleep 5printf times cmd: timesprintf pipeline: times | ( read user sys; echo $user; )printf head: times | head -n1printf times cmd again: timeswith the following output:$ ./test.sh times cmd: 0m0.003s 0m0.005s0m0.001s 0m0.001spipeline: 0m0.000shead: 0m0.000s 0m0.000stimes cmd again: 0m0.003s 0m0.006s0m0.003s 0m0.004sQuestion is, why times command resets time when used with a pipeline? Is there any way to avoid it in order to parse the value?
Why builtin times doesn't work in pipeline?
bash;shell;shell script;pipe;shell builtin
Within a pipeline, all commands are run in a subshell. times reports time spent by the shell and its subshells, but not its parent shell.You can try process substitution instead:times > >( head -n1 )times > >( read user sys ; echo $user )