tags:

views:

129

answers:

1

Hey,

The problem is when I use time in shell I get output like that:

1.350u 0.038s 0:01.45 95.1%     0+0k 0+72io 1pf+0w

And when Im using it in script I get:

real    0m1.253s
user    0m1.143s
sys     0m0.047s

I mean why? And in shell script at the beginning I write:

#!/bin/bash
+2  A: 

Bash has a built-in command time, and your system should also have a separate binary at /usr/bin/time:

$ help time
time: time [-p] pipeline
    Report time consumed by pipeline's execution.

    Execute PIPELINE and print a summary of the real time, user CPU time,
    ...
$ which time
/usr/bin/time

The two commands produce different outputs:

$ time echo hi
hi

real    0m0.000s
user    0m0.000s
sys 0m0.000s
$ /usr/bin/time echo hi
hi
0.00user 0.00system 0:00.00elapsed 100%CPU (0avgtext+0avgdata 0maxresident)k
0inputs+0outputs (0major+199minor)pagefaults 0swaps
Mark Rushakoff
Yes you're right but why script is using different command than it's shell?
l245c4l
the /usr/bin/time binary is not in the path when running the script, but it is when you are in the shell. Or you are not running bash shell in the command line?
Kimvais
Now Im really confused. I echo'ed $SHELL and I got /bin/tcsh, but I still think its bash
l245c4l
Do a `type -a time` in Bash: It will show you everything executable that Bash knows of that is named `time`. In my case, it says `time is a shell keyword` and `time is /usr/bin/time`. I recommend that people prefer using `type` over `which` in Bash. (We used to have problems years ago because `which` used the C shell, and not Bash, and since we didn't support C shell internally, its `$PATH` tended to be a system default and incorrect.)
Mike DeSimone
Yes you're right it gives me:$ type -a timetime is a shell keywordtime is /usr/bin/time
l245c4l
Also, if your script has `#!/bin/sh` as its shebang or none at all, it's likely going to be run using a shell other than Bash.
Dennis Williamson
Another way to run the non-builtin `time` in sh-like shells (like Bash) is, instead of specifying a full path, use `\time` or `command time`.
ephemient