-3

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 ....

  • 1
    First tries builtin, then tries external. which is not a shell (bash) command. use type cd, or type echo, which is a builtin shell (bash) command. – Ipor Sircer Jul 24 '18 at 04:39
  • @Ipor Sircer , do you mean to say that the shell itself comes with the code to execute the builtin programs and thus is faster ? – Natesh bhat Jul 24 '18 at 04:52
  • 2
    We have done this over and over, with https://unix.stackexchange.com/questions/59171/ , https://unix.stackexchange.com/questions/38808/ , https://unix.stackexchange.com/questions/116955/ , https://unix.stackexchange.com/questions/358622/ , https://unix.stackexchange.com/questions/354123/ , and https://unix.stackexchange.com/questions/50058/ . And that's just the ones with cd. There are yet more with echo. – JdeBP Jul 24 '18 at 07:29

1 Answers1

3

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).

Kusalananda
  • 333,661
  • 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 07:35