Can I write the output of the time difference command to a file?
For example, I tried:
$ time foo.sh > bar.txt
But it only gives the output of foo.sh to bar.txt.
bashio-redirectionshell-scripttime
Can I write the output of the time difference command to a file?
For example, I tried:
$ time foo.sh > bar.txt
But it only gives the output of foo.sh to bar.txt.
Best Answer
In many shells including
ksh
,zsh
andbash
,time
is a keyword and is used to time pipelines.Will time both the
foo
andbar
commands (zsh
will show you the breakdown). It reports it on the shell's stderr.Will tell you the time needed to open
bar.txt
and runfoo.sh
.If you want to redirect
time
's output, you need to redirectstderr
in the context wheretime
is started like:This:
works as well but with
ksh93
andbash
, because it's not in first position,time
is not recognised as a keyword, so thetime
command is used instead (you'll probably notice the different output format, and it doesn't time pipelines).Note that both would redirect both the output of time and the errors of
foo.sh
tobar.txt
. If you only want the time output, you'd need:Note that POSIX doesn't specify whether
time
behaves as a keyword or builtin (whether it times pipelines or single commands). So to be portable (in case you want to use it in ash
script which you want portable to different Unix-like systems), you should probably write it:Note that you can't time functions or builtins or pipelines or redirect
foo.sh
errors separately there unless you start a separate shell as in:But that means the timing will also include the startup time of that extra
sh
.