Linux – Redirect output of time command in unix into a variable in bash

bashlinux

I just want to capture the output of a time command i.e:

X=$(time ls)

or

$(time ls) | grep real

The time function spits it to the console though. How do I do this?

Best Answer

X=`(time ls) 2>&1 | grep real`