17

I wanted to use gnu time to measure running time of some little .c programs. In the man it is written that:

-f FORMAT, --format FORMAT
Use FORMAT as the format string that controls the output of time.  See the below more information.

Then in examples we have:

To run the command `ls -Fs' and show just the user, system, and total time:
time -f "%E real,%U user,%S sys" ls -Fs

But when I try to issue this command from example i get:

time -f '%E real,%U user,%S sys' ls -Fs
-f: command not found

real    0m0.134s
user    0m0.084s
sys     0m0.044s

I am wondering where is the problem, where am I making a mistake? I just want to show the user time, that is why I am toying with time output format.

Andna
  • 6,539
  • 13
  • 71
  • 120

2 Answers2

18

Bash for one has a shell builtin named time. One way to get past it is to type command time - command will ignore the builtins and run the time program from your $PATH. Another way is alias time=/usr/bin/time. On the other hand the bash builtin respects environment variable TIMEFORMAT.

Kevin Burke
  • 61,194
  • 76
  • 188
  • 305
minopret
  • 4,726
  • 21
  • 34
  • Thanks for quick answer, I will just use TIMEFORMAT, but all of those work. – Andna Mar 15 '12 at 07:53
  • 2
    `/usr/bin/time` isn't available on some systems (e.g. NixOS), but I've found that `command time ...` works as a way to avoid the bash builtin. – Warbo Apr 21 '17 at 22:32
  • Great point, `command` is a POSIX-defined shell utility so it ought to be available in `sh` and in anything (including `bash` and `ksh`) that can act as `sh`. As was mentioned here: https://stackoverflow.com/questions/6365795/invoking-program-when-a-bash-function-has-the-same-name – minopret Apr 25 '17 at 23:17
1

The documentation also mentions env time to use the time command from the system (it uses /usr/bin/env or alike, so it should be independent of the shell).

tobiasBora
  • 1,542
  • 14
  • 23