emperor: (Default)
Add MemoryShare This Entry
"time" is a reserved keyword in bash, not a builtin as one might naively expect[1,2]. Even more amusingly, many UN*X systems also have /usr/bin/time which has rather less nice output.

This leads to some irritating edge-cases if you forget this (or didn't realise it in the first place). Consider:

bash-3.2$ time sleep 1

real    0m1.009s
user    0m0.000s
sys     0m0.002s
bash-3.2$ FOO=bar time sleep 1
        1.00 real         0.00 user         0.00 sys
bash-3.2$ time FOO=bar sleep 1

real    0m1.002s
user    0m0.000s
sys     0m0.002s


...and things only get worse if you actually start chaining things together...

bash-3.2$ time sleep 1 | time sleep 4
        4.00 real         0.00 user         0.00 sys

real    0m4.004s
user    0m0.001s
sys     0m0.005s


Not perhaps the best design decision ever...

[1] #chiark thinks this was to make it possible to time entire pipelines, although there would be several obvious idioms for doing that if time were a builtin
[2] See the "Pipelines" section in bash(1) for more details
There are 3 comments on this entry. (Reply.)
sparrowsion: photo of male house sparrow (string-handling kitten)
posted by [personal profile] sparrowsion at 03:16pm on 22/06/2009
Oh, that explains the behaviour of:
$ nice -19 time ./job-which-times-subjobs.sh
I've been running into recently.
ext_243: (bubbles)
posted by [identity profile] xlerb.livejournal.com at 05:09pm on 22/06/2009
Indeed, in zsh:

$ time sleep 1 | { cat; sleep 1 } | cat
sleep 1 0.00s user 0.00s system 0% cpu 1.001 total
{; cat; sleep 1; } 0.00s user 0.00s system 0% cpu 2.003 total
cat 0.00s user 0.00s system 0% cpu 2.003 total
 
posted by [identity profile] wellinghall.livejournal.com at 05:37pm on 22/06/2009
I read the first line of your code as "bath time sleep" ...

July

SunMonTueWedThuFriSat
    1
 
2
 
3 4
 
5
 
6
 
7
 
8
 
9 10
 
11
 
12
 
13
 
14
 
15
 
16
 
17
 
18
 
19
 
20
 
21
 
22
 
23
 
24
 
25
 
26
 
27
 
28
 
29
 
30
 
31
 
   
OSZAR »