3

Is test a shell builtin or actual program?

bruce@Bruce:~$ type -a test
test is a shell builtin
test is /usr/bin/test
bruce@Bruce:~$

bruce@Bruce:~$ file /usr/bin/test
/usr/bin/test: ELF 64-bit LSB executable
bruce@Bruce:~$ 

My system is running Ubuntu 13.04 and BASH 4.2.45(1)-release.

1

4 Answers 4

9

Seems that I doesn't really got your question on my first approach. Will try to explain it further...

Although the program test is part of the GNU coreutils package and will therefore being delivered with any GNU/Linux system, it is also compiled as a builtin into bash in most distributions. I guess that this is to improve performance as it does not need to start a child process for every conditional statement. Needless to say that the shell will use the builtin in favour of the binary if both are present on the system. But you can call:

/usr/bin/test ...

... if you want to explicitly call the binary.

Further note that bash is not the only shell on most systems and other shells may don't have this builtin feature (like dash for example). Also there are systems which may not even have bash installed. For such situations there is the binary from coreutils.


It is likely that it is a builtin on your system. To check this type:

help test

If you see a help page it is a builtin

Also you can type:

type -t test

This shows:

builtin 

on my system

4
  • Why is there a file named test on the file system then?
    – Bruce Blacklaws
    Sep 5, 2013 at 8:00
  • This is because the coreutils package contains one
    – hek2mgl
    Sep 5, 2013 at 8:02
  • Which one gets run then?
    – Bruce Blacklaws
    Sep 5, 2013 at 8:02
  • 1
    The builtin.. Note: You will find even a binary called /bin/[ .. But there is also a builtin called [
    – hek2mgl
    Sep 5, 2013 at 8:03
1

test is a virtual command inside bash or what you could call a builtin. /usr/bin/test exists as well as an external command. Along with builtins and commands there are also functions which you can think of more like custom builtins. The priority on how they are called are: functions first, builtins second, external binaries or commands last; but you could customize that.

If you have somehow made a function that has the same name as a builtin e.g. cd, to skip calling the function and call the builtin cd instead, you could use the builtin command builtin e.g. builtin cd args.

Likewise if you have a builtin that has same name as a command stored in the filesystem e.g. test, to call the command test instead, either you give the explicit path e.g. /usr/bin/test or use the builtin command command e.g. command test -n xyz.

To get more idea about it, try running these commands:

help
help builtin
help command
1

Historically, test (and its synonym [) were (and remain) external programs. But most shells provide them as built-in commands for efficiency. The same is true for common commands like true, false, printf, echo, etc.

0

As a complement to previous answers, one can use the builtin command enable to enable [or disable] the builtin version of a given command cmd (e.g. test), by enable [-n] cmd. The documentation of enable takes precisely test as an example on my system.

enable -a shows the list of builtin commands and their activation state.

You must log in to answer this question.