[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
why does time (cmd) 2> file redirect time's output?
From: |
Stephane Chazelas |
Subject: |
why does time (cmd) 2> file redirect time's output? |
Date: |
Wed, 27 Jan 2016 10:59:16 +0000 |
User-agent: |
Mutt/1.5.21 (2010-09-15) |
Hello,
(tested with bash-4.3 and bash-2.05b)
"time" is meant to time a pipeline. That generally includes
redirections of the last command in the pipeline, but in the
case of a subshell alone, the redirection is not timed(*) and
seems to affect the output of "time" itself.
That's quite inconsistent, and different from other shells.
$ bash -c 'time (uname) 2> /dev/null'
Linux
$ bash -c 'time uname 2> /dev/null'
Linux
real 0m0.002s
user 0m0.000s
sys 0m0.001s
$ bash -c 'time { uname;} 2> /dev/null'
Linux
real 0m0.001s
user 0m0.000s
sys 0m0.001s
$ bash -c 'time (uname) | wc -c 2> /dev/null'
6
real 0m0.002s
user 0m0.000s
sys 0m0.003s
$ bash -c 'time (uname) | (wc -c) 2> /dev/null'
6
real 0m0.003s
user 0m0.000s
sys 0m0.003s
$ bash -c 'time (uname) 2> /dev/null | (wc -c) 2> /dev/null'
6
real 0m0.002s
user 0m0.001s
sys 0m0.002s
$ ksh -c 'time (uname) 2> /dev/null'
Linux
real 0m0.00s
user 0m0.00s
sys 0m0.00s
$ zsh -c 'time (uname) 2> /dev/null'
Linux
( uname; ) 2> /dev/null 0.00s user 0.00s system 89% cpu 0.002 total
$ mksh -c 'time (uname) 2> /dev/null'
Linux
0m0.00s real 0m0.00s user 0m0.00s system
Is there a reason for that?
Note that it may be too late to change it as some people seem to
rely on it. See for instance:
https://unix.stackexchange.com/questions/12068/how-to-measure-time-of-program-execution-and-store-that-inside-a-variable/12069#12069
But it could be worth documenting.
(*)
well, time (cmd) < <(busy-cmd)
does seem to time "busy-cmd".
--
Stephane
- why does time (cmd) 2> file redirect time's output?,
Stephane Chazelas <=