Internal and external commands in Linux

On a Linux shell, you have two types of commands: internal and external. Internal commands are shell in-built. They execute directly from the shell. External commands are stored on the disk. The shell executes them from the disk.

How a Linux shell executes commands

When you hit the Enter key after typing a command on a Linux shell, the shell takes the following steps.

In the first step, it checks whether the typed command is an alias for an existing command or script. If it is an alias, it executes the command or script specified in the alias. If it is not an alias, it takes the next step.

In the second step, it checks whether the typed command is an internal command. If it is an internal command, it executes it from the memory. When you start a Linux shell, the shell copies all internal commands to RAM and executes them from there. If the typed command is not an internal command, it takes the next step.

In the third step, it checks whether the typed command is an external command. To know whether the typed command is an external command, it uses the PATH variable. The PATH variable contains the location of the directories having external commands' files. The shell checks all directories specified in the PATH variable. If it finds a file matching the typed command in any directory, it copies the file into RAM and executes it from there. If the user executes the same command again, it executes it from RAM. If the shell does not find a file related to the typed command in any directory, it returns the 'command not found' error.

Knowing the type of a command

To know whether a command is an internal command or an external command, you can use the type and which commands. The which command displays the location from where the shell executes the command. The type command displays the type of the command.

Let's take some examples.

Use the which command to print the location from where the shell executes the ll, echo, and cat commands.

#which ll
#which echo
#which cat

which command

Now use the type command to display the type of the same commands.

#type ll
#type cat
#type host

type command

Viewing the location of external commands

To view the location of directories having external commands, you can check the value of the PATH variable. This variable defines a list of directories that the shell searches for a matching filename when a user enters a command.

The following command displays the value of the PATH variable.

#echo $PATH

path command

Differences between internal and external commands

The following table lists the main differences between internal and external commands.

Internal commands are part of the shell.External commands are not part of the shell.
Internal commands execute fast as they directly run from the shell.External commands are slow as they run from the disk.
Internal commands provide only basic functionalities.External commands provide all functionalities.
Internal commands are simple and straightforward.External commands are complex.
Internal commands always run from RAM.External commands first time run from disk.

ComputerNetworkingNotes Linux Tutorials Internal and external commands in Linux