Reputation: 34286
With bash, how do I store the time it takes for my program to execute in one variable, and the output of my program in another variable? I know that time
sends its output to stdout
; the closest I've got is this:
exec 3>&1 4>&2
time_output=$( { time echo hello world 1>&3 2>&4; } 2>&1 )
but "hello world" is printed to the terminal. How to I capture "hello world" to another variable?
I've also tried:
prog_output=$(time_output=$( { time echo hello world 1>&3 2>&4; } 2>&1 ) )
but this doesn't work. prog_output
contains nothing and "hello world" is printed to the terminal.
Upvotes: 1
Views: 135
Reputation: 11163
Would this work for you?
prog_output=`( time ls ) 2> time_output`
time_output=`cat time_output`
Upvotes: 1