How can I redirect the output of the “time” command?

前端 未结 7 1528
甜味超标
甜味超标 2020-11-30 19:19

I tried to redirect the output of the time command, but I couldn\'t:

$time ls > filename
real    0m0.000s
user    0m0.000s
sys     0m0.000s
7条回答
  •  Happy的楠姐
    2020-11-30 19:39

    The reason why redirection does not seem to work with time is that it's a bash reserved word (not a builtin!) when used in front of a pipeline. bash(1):

    If the time reserved word precedes a pipeline, the elapsed as well as user and system time consumed by its execution are reported when the pipeline terminates.

    So, to redirect output of time, either use curly braces:

    { time ls; } 2> filename
    

    Or call /usr/bin/time:

    /usr/bin/time ls 2> filename
    

提交回复
热议问题