You can get the full output time echo "hi"
, and save this to a variable:
stats=$({ time echo "hi"; } 2>&1)
or to a file:
{ time echo "hi"; } >mylogfile.txt 2>&1
The brace syntax here lets bash know what actual information you want. Because time is a reserved word in bash (like if
or while
), as well as a built-in, redirecting stdout
or stderr
without braces will not redirect the output from time
. To do that, we need to group all the output from the command together, and then redirect it.
However, if you only want the timing info, we can redirect the command's information to the void /dev/null
and keep only time
's output. We can save this to a variable:
stats=$({ time echo "hi" 1>/dev/null 2>&1; } 2>&1)
or to a file:
{ time echo "hi" 1>/dev/null 2>&1; } 2>mylogfile.txt