[NTLUG:Discuss] 'time'ing compound commands.

Patrick R. Michaud pmichaud at pobox.com
Sun Oct 16 21:42:54 CDT 2005


On Sun, Oct 16, 2005 at 05:02:30PM -0500, Richard Geoffrion wrote:
> Can someone explain to me why a command like....
>     time ( w; ls )
> ...doesn't "time"?   Oh sure, they both run, but they aren't timed.  Is 
> there a way to do this without resorting to the setting of variables 
> using `date +s`..and other such bash scripting?

"time" is a shell reserved word that expects an executable command
after it, the parens produce a "subshell" that "time" doesn't
seem to know how to deal with.

On my system, braces seem to work:

    [pmichaud at power t]$ time { w; ls; }
     21:41:29 up 7 days, 16:24,  4 users,  load average: 0.35, 0.37, 0.32
    USER     TTY      FROM              LOGIN@   IDLE   JCPU   PCPU WHAT
    pmichaud pts/0    c-29-9-29-255.hs 21:27    1:34   0.34s  0.15s bash
    pmichaud pts/1    c-29-9-29-255.hs 10:08    7:44m  0.73s  0.71s w
    alpha  beta
    
    real    0m0.039s
    user    0m0.020s
    sys     0m0.000s

Or you can put the commands into a function and then time that:

    function wls { w; ls; }
    time wls

Note that inside of curly braces the trailing semicolon is required.

Hope this helps,

Pm




More information about the Discuss mailing list