Bash – Make bash use external `time` command rather than shell built-in

bashshell-builtintime-utility

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

Best Answer

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.