Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

How can I make bash use time binary (/usr/bin/time) by default instead of the shell keyword?

which time returns /usr/bin/time
type time returns time is a shell keyword
Running time is obviously executing the shell keyword:

$ time

real    0m0.000s
user    0m0.000s
sys     0m0.000s
$ /usr/bin/time
Usage: /usr/bin/time [-apvV] [-f format] [-o file] [--append] [--verbose]
   [--portability] [--format=format] [--output=file] [--version]
   [--quiet] [--help] command [arg...]

enable -n time returns bash: enable: time: not a shell builtin

share|improve this question

4 Answers 4

up vote 14 down vote accepted

You can use the command shell built-in to bypass the normal lookup process and run the given command as an external command regardless of any other possibilities (shell built-ins, aliases, etc.). This is often done in scripts which need to be portable across systems, although probably more commonly using the shorthand \ (as in \rm rather than command rm or rm, as especially the latter may be aliased to something not known like rm -i).

$ time

real    0m0.000s
user    0m0.000s
sys 0m0.000s
$ command time
Usage: time [-apvV] [-f format] [-o file] [--append] [--verbose]
       [--portability] [--format=format] [--output=file] [--version]
       [--quiet] [--help] command [arg...]
$ 

This can be used with an alias, like so:

$ alias time='command time'
$ time
Usage: time [-apvV] [-f format] [-o file] [--append] [--verbose]
       [--portability] [--format=format] [--output=file] [--version]
       [--quiet] [--help] command [arg...]
$ 

The advantage of this over e.g. alias time=/usr/bin/time is that you aren't specifying the full path to the time binary, but instead falling back to the usual path search mechanism.

The alias command itself can go into e.g. ~/.bashrc or /etc/bash.bashrc (the latter is global for all users on the system).

For the opposite case (forcing use of the shell built-in in case there's an alias defined), you'd use something like builtin time, which again overrides the usual search process and runs the named shell built-in. The bash man page mentions that this is often used in order to provide custom cd functionality with a function named cd, which in turn uses the builtin cd to do the real thing.

share|improve this answer
    
You can also inquire where a particular command is coming from using the command type -a <cmd>. –  slm Aug 10 '13 at 18:29
2  
command doesn't bypass builtins (except in zsh when not emulating other shells). It bypasses functions and that's the main reason for its existence. Its second role is to remove the special in special builtins. It bypasses aliases and keywords only because those are only expanded in first position (in zsh for instance, it doesn't bypass global aliases). There is no time bash builtin. time is a keyword so you can do for instance time { foo; bar; }. –  Stéphane Chazelas Aug 10 '13 at 21:14
    
@StephaneChazelas By all means feel free to edit that into the answer to provide a more accurate description. command works in this particular case, as exemplified, but obviously a correct answer is better than a half-right one. –  Michael Kjörling Aug 11 '13 at 10:44

There is a shortcut in bash to sidestep keywords, without having to specify a path or use another builtin like command: Escape it with a backslash.

=^_^= izkata@Izein:~$ time

real    0m0.000s
user    0m0.000s
sys     0m0.000s
=^_^= izkata@Izein:~$ \time
Usage: time [-apvV] [-f format] [-o file] [--append] [--verbose]
       [--portability] [--format=format] [--output=file] [--version]
       [--quiet] [--help] command [arg...]

Personally, I find this more readable and safer, as this is possible:

=^_^= izkata@Izein:~$ alias command=echo
=^_^= izkata@Izein:~$ command time
time
share|improve this answer
5  
Note that any form of quoting or anything that makes it not the token time will do: ti\me, 'time', ti""me, ti${-##*}me, ${-+time}... That applies to any shell keyword. –  Stéphane Chazelas Aug 10 '13 at 20:59

Drop this into your ~/.bashrc:

alias time=/usr/bin/time

share|improve this answer

You can use the enable command to disable certain built-ins.

$ enable -n kill

However time is a keyword so this won't work.

$ builtin time
bash: builtin: time: not a shell builtin

So you're left with creating an alias to override it:

$ alias time=/usr/bin/time
share|improve this answer
    
nope. time is a shell keyword, not a shell builtin. I was just about to edit my question to say that enable -n does not work when I saw you had answered. –  David Holdeman Aug 10 '13 at 15:45

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.