Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

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.

share|improve this question

migrated from stackoverflow.com Sep 5 '13 at 8:20

This question came from our site for professional and enthusiast programmers.

    
This comment my help you: stackoverflow.com/questions/17487514/… –  fedorqui Sep 5 '13 at 7:57

3 Answers 3

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

share|improve this answer
    
Why is there a file named test on the file system then? –  Bruce Blacklaws Sep 5 '13 at 8:00
    
This is because the coreutils package contains one –  hek2mgl Sep 5 '13 at 8:02
    
Which one gets run then? –  Bruce Blacklaws Sep 5 '13 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 '13 at 8:03

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
share|improve this answer

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.

share|improve this answer

Your Answer

 
discard

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