Time multiple commands without a subshell?

236 views Asked by At

Is there a way to use the time reserved word in zsh to time multiple commands, without starting a subshell?

I know that this works:

{ time (
    sleep 5
    sleep 3
    PROMPT='foobar> '
) }

However the parentheses mean that a subshell is created, and variables initialized don't get exported.

I know I can capture the variables before and after, like

start=$(time)
# do something
end=$(time)
echo start - end | bc

Though for ad hoc timing this is a little cumbersome.

1

There are 1 answers

0
vinc17 On BEST ANSWER

No, time can only work on a different process. So, it won't work with { ... } or with a builtin, like:

time { ls }
time echo

Note that your method capturing the time output won't work if there are already children (as their times when running the commands will also be taken into account). Ditto if you have traps and corresponding signals occur.