Redirect the output of a command in `time command`
In ksh, bash and zsh, time
is a keyword, not a builtin. Redirections on the same line apply only to the command being timed, not to the output of time
itself.
$ time ls -d / /nofile >/dev/null 2>/dev/null
real 0m0.003s
user 0m0.000s
sys 0m0.000s
To redirect the output from time
itself in these shells, you need to use an additional level of grouping.
{ time mycommand 2>&3; } 3>&2 2>mycommand.time
If you use the GNU version of the standalone time
utility, it has a -o
option to write the output of time
elsewhere than stderr. You can make time
write to the terminal:
/usr/bin/time -o /dev/tty mycommand >/dev/null 2>/dev/null
If you want to keep the output from time
on its standard error, you need an extra level of file descriptor shuffling.
/usr/bin/time -o /dev/fd/3 mycommand 3>&2 >/dev/null 2>/dev/null
With any time
utility, you can invoke an intermediate shell to perform the desired redirections. Invoking an intermediate shell to perform extra actions such as cd
, redirections, etc. is pretty common — it's the kind of little things that shells are designed to do.
/usr/bin/time sh -c 'exec mycommand >/dev/null 2>/dev/null'