Internal and External commands in Linux

The Linux shell includes two variations of commands: internal and external. Internal commands are shell built-in. They execute directly from the shell. External commands are package-specific. Many packages provide specific commands to control and manage their functions. These commands are known as external commands. The installation process installs them with the packages under pre-defined locations on the Linux file system. The shell executes them from their installation location.

How a Linux shell executes commands

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

  1. 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 not, it takes the next step.
  2. It checks whether the typed command is internal. If yes, it executes the command from memory. When we start a Linux shell, it copies all internal commands to RAM and executes them from there. If not, it takes the next step.
  3. It checks whether the typed command is external. It uses the PATH variable to determine whether the typed command is external. The PATH variable contains the location of the directories that have files for external commands. 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 we execute 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.

how the shell runs commands

Knowing the type of commands

Linux offers two commands to display the command type. These commands are: type and which . The type command shows the type of the command. The which command shows the location from which the command runs. Both commands accept the command name as an argument.

internal and external commands

If the given command is internal, the type command shows that the command is a shell built-in. If it is an external command, it shows the location from which the shell runs the command. The which command shows the path of the command file for both types. For an external command, the output of both commands is identical.

the type command

Let's take some examples.

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

#which ll
#which echo
#which cat

the which command examples

Now, use the type command to display the type of the above commands.

#type ll
#type cat
#type host

the type command examples

Viewing the location of external commands

To view the location of directories having external commands, we use 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

the path command

Listing all internal commands

The compgen -b command lists all shell built-in commands.

listing all internal commands

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 faster as they are run directly 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 straightforward. External commands are complex.
Internal commands always run from RAM. External commands, for the first time, run from disk.

Conclusion

Understanding the differences between internal and external commands is essential for efficient command line usage. Internal commands are faster and provide basic functionalities by executing directly from RAM. In contrast, external commands are more complex and relatively slower. However, they perform nearly all functions of a typical Linux system.

ComputerNetworkingNotes Linux Tutorials Internal and External commands in Linux

We do not accept any kind of Guest Post. Except Guest post submission, for any other query (such as adverting opportunity, product advertisement, feedback, suggestion, error reporting and technical issue) or simply just say to hello mail us ComputerNetworkingNotes@gmail.com