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.

If I execute the test command in bash, test(evaluates conditional expression) built-in utility is started:

$ type test
test is a shell builtin
$ type -a test
test is a shell builtin
test is /usr/local/bin/test
test is /usr/bin/test
$ 

However, as seen in output of type -a test above, there is another test in /usr/local/bin directory and yet another one in /usr/bin directory. How are executables ordered, i.e. are the built-in commands always preferred and then the rest of the commands depend on the directory order in $PATH variable? In addition, is it possible to change the order of the executables started, e.g. if I type in test, then /usr/bin/test is started instead of bash-builtin test?

share|improve this question
    
You can specify the full path when calling the command, eg., /usr/bin/test -f "$file"... –  jasonwryan May 29 at 22:43
    
@jasonwryan I'm aware of this, but I'm just interested if there is a way to change the order of executables started. –  Martin May 29 at 22:48

2 Answers 2

Highest priority is bash alias, then special builtins (only in POSIX mode), then functions, then builtins, then a search in $PATH.

To execute a builtin, use builtin test.
To execute an external application, use an explicit path: /bin/test.
To ignore functions and aliases, use command test.
To bypass just alias, use \test or any other kind of expansion.

It's possible to disable/enable a builtin with enable test.

(Updated according to comments below)
(Fixed incorrect admin edit that bash has disable builtin - in fact, there is only enable)

share|improve this answer
    
The order you have specified is different than the order mentioned in an answer from one of the gurus on this site. Which one is right? –  1_CR May 29 at 23:21
1  
@1_CR gena2x is right. My answer omitted special builtins, which take precedence over functions as per POSIX (though some shells are not compliant; bash complies only in POSIX mode). –  Gilles May 29 at 23:55
    
@gilles, cool. +1 to gena2x –  1_CR May 29 at 23:59
1  
It's builtin, not buildin. –  nyuszika7h May 30 at 9:26
    
And to bypass just the alias : \test (this will either start the builtin, or, if no builtin of that name or it is disabled, will start any function of that name, or look for it in the PATH) –  Olivier Dulac May 30 at 10:12

Built-in commands are always preferred to external commands. The rationale is that the built-in command is faster (and in a few cases, such as cd or test -o BASH_OPTION, only the built-in command can have the desired effect).

Sometimes the external command may have capabilities that the shell builtin doesn't. In that case, you can call the external command by giving an explicit path (i.e. containing a slash) (this bypasses any concern about the order in $PATH). If you don't want to hard-code the external path but you do want to prevent the use of the builtin, you can use "$(type -P test)" (note capital P) in bash, "$(whence -p test)" in ksh, and =test in zsh.

In zsh, you can disable a builtin with disable test. This is permanent (for the current shell or subshell) until the builtin is reenabled with enable test. In bash, you can do the same with enable -n test to disable and enable test to reenable.

You can use an alias or function to force the execution of a different command, for example alias test=/usr/bin/test or test () { /usr/bin/test "$@"; }. If you have such an alias, you can prevent its use by quoting any part of it, e.g. \test will perform the normal function/builtin/external lookup. Note that depending on the shell and its settings, alias definitions in a function may be expanded when a function is read or when it is executed. If you've defined a function, you can use command test to prevent function lookup as well as alias lookup (so here the test builtin will be invoked unless disabled).

share|improve this answer

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.