I was reading the marked answer from here : What is the difference between a builtin command and one that is not?

Here he says :

A built-in command is simply a command that the shell carries out itself, instead of interpreting it as a request to load and run some other program. This has two main effects. First, it's usually faster, because loading and running a program takes time. Of course, the longer the command takes to run, the less significant the load time is compared to the overall run time

Does it mean that when we run shell builtin commands like cd or echo , the shell (lets say bash) interprets it differently than running the program cd from the path (/usr/bin/cd) which is obtained by using which cd.

I had thought that all commands that we execute run the corresponding programs. Seeing that answer has confused me.

If the shell doens't execute the program or file , then why does which cd still returns the path of that program ? Doesn't it imply that when we run echo hi command , it searches the paths in PATH env variable to execute a program by name echo ?

Please explain this ....


The shell knows what utilities are built-ins.

The bash shell finds the command using these steps (once alias expansion has been performed):

  1. If the command contains no slashes
    1. If the command is a shell function, that function is called.
    2. Otherwise, if it corresponds to a built-in command, that command is used.
    3. Otherwise, the shell searches $PATH for an executable with that name and executes it if found.
  2. If the command contains slashes, that named file is executed.

This means that the only time that the shell searches for executables in $PATH is when the command contains no slashes, is not a shell function and is not a built-in utility. The bash shell furthermore will not do a physical search of the directories if the given command has already been hashed (see help hash in bash).

  • Note that in posix mode, special builtins take precedence over functions (but you can only declare a function by the same name as a special builtin when not in posix mode, so it only applies in things like export() { x;}; set -o posix; export) – Stéphane Chazelas Jul 24 '18 at 7:35

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

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