28

Is there a way to time a series of commands in linux?

Perhaps something like this:

/usr/bin/time { ls; pwd; ls }
1
  • even without the time command this would fail as it needs the final ; i.e. { ls ; pwd ; ls ; } Nov 3, 2018 at 19:07

2 Answers 2

13

You could try to wrap it in a shell command:

/usr/bin/time /bin/sh -c 'ls;pwd;ls'
0
42

Using time use () to create a subshell for the commands you wish to time. The syntax would be:

time ( ls; pwd; ls )

If using /usr/bin/time then add the commands to a shell script (for example my.sh) and time the shell script-:

/usr/bin/time my.sh
3
  • 3
    This works with time but not with /usr/bin/time. Running the command you gave produces -bash: syntax error near unexpected token ls'`
    – juniper-
    Jun 17, 2013 at 12:32
  • Although the accepted answer at unix.stackexchange.com/a/46106/419658 works great, I still prefer this version because it makes the code much readable, instead of just being passed as a string to sh -c. Anyway, Can somebody please point a situation when one would be preferred over another? Aug 13, 2020 at 1:17
  • I can see that this works on bash and zsh, but not on sh and fish. I guess this may answer your question as to whether we should prefer one or the other: portability.
    – BenMorel
    May 19, 2021 at 13:40

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .