Internal and External Commands in Linux
Last Updated :
03 Jul, 2020
The UNIX system is command-based i.e things happen because of the commands that you key in. All UNIX commands are seldom more than four characters long.
They are grouped into two categories:
- Internal Commands : Commands which are built into the shell. For all the shell built-in commands, execution of the same is fast in the sense that the shell doesn’t have to search the given path for them in the PATH variable, and also no process needs to be spawned for executing it.
Examples: source, cd, fg, etc.
- External Commands : Commands which aren’t built into the shell. When an external command has to be executed, the shell looks for its path given in the PATH variable, and also a new process has to be spawned and the command gets executed. They are usually located in /bin or /usr/bin. For example, when you execute the “cat” command, which usually is at /usr/bin, the executable /usr/bin/cat gets executed.
Examples: ls, cat etc.
If you know about UNIX commands, you must have heard about the ls command. Since ls is a program or file having an independent existence in the /bin directory(or /usr/bin), it is branded as an external command that actually means that the ls command is not built into the shell and these are executables present in a separate file. In simple words, when you will key in the ls command, to be executed it will be found in /bin. Most commands are external in nature, but there are some which are not really found anywhere, and some which are normally not executed even if they are in one of the directories specified by PATH. For instance, take echo command:
$type echo
echo is a shell builtin
echo isn’t an external command in the sense that, when you type echo, the shell won’t look in its PATH to locate it(even if it is there in /bin). Rather, it will execute it from its own set of built-in commands that are not stored as separate files. These built-in commands, of which echo is a member, are known as internal commands.
You now might have noticed that it’s the shell that actually does all these works. This program starts running as soon as the user log in and dies when the user logs out. The shell is an external command with a difference, it possesses its own set of internal commands. So, if a command exists both as an internal command of the shell as well as external one(in /bin or /usr/bib), the shell will accord top priority to its own internal command of the same name.
This is exactly the case with echo which is also found in /bin, but rarely ever executed because the shell makes sure that the internal echo command takes precedence over the external. Now, talk more about the internal and external commands.
Getting the list of Internal Commands
If you are using bash shell you can get the list of shell built-in commands with help command :
$help
// this will list all
the shell built-in commands //
How to find out whether a command is internal or external?
In addition to this you can also find out about a particular command i.e whether it is internal or external with the help of type command :
$type cat
cat is /bin/cat
//specifying that cat is
external type//
$type cd
cd is a shell builtin
//specifying that cd is
internal type//
Internal vs External
The question that when to use which command between internal and external command is of no use cause the user uses a command according to the need of the problem he wants to solve. The only difference that exists between internal and external commands is that internal commands work much faster than the external ones as the shell has to look for the path when it comes to the use of external commands.
There are some cases where you can avoid the use of external by using internal in place of them, like if you need to add two numbers you can do it as:
//use of internal command let
for addition//
$let c=a+b
instead of using :
//use of external command expr
for addition//
$c=`expr $a+$b`
In such a case, the use of let will be a better option as it is a shell built-in command so it will work faster than the expr which is an external command.
Similar Reads
Interesting Funny Commands in Linux
This entire article is all about showing off cool commands executable in the Linux terminal. This article is aimed at being a one-stop destination for all cool or you can say interesting funny commands in Linux to play. 1. The Matrix Effect: We all can agree on one thing that the falling down text e
5 min read
Kali Linux - Command Line Essentials
Command-line plays a vital role while working with Kali Linux as most of its tools don't have a Graphical User Interface and if you are performing ethical hacking or penetration testing then most of the time you will have to work with command Line Interface itself. While executing a command in Kali
5 min read
info command in Linux with Examples
info command reads documentation in the info format. It will give detailed information for a command when compared with the man page. The pages are made using the Texinfo tools which can link with other pages, create menus, and easy navigation. Here, we will explore the functionality of the info com
3 min read
as command in linux with examples
as command is the portable GNU assembler in Linux. Using as command, we can read and assemble a source file. The main purpose of 'as' is to assemble the output of the GNU compiler of the C language.as command reads and assembles the .s File. Also, whenever you do not specify the file, it normally re
3 min read
ip Command in Linux with Examples
The ip command in Linux is a powerful utility for network configuration and management. It allows users to interact with various networking components such as network interfaces, routing tables, addresses, and more. Here, we will look into the 'ip' command, covering each aspect with examples, code,
7 min read
ipcrm command in Linux with examples
ipcrm command in Linux is used to remove some IPC(Inter-Process Communication) resources. It eliminates the IPC objects and their associated data structure form the system. One must be a creator or superuser or the owner of the object in order to remove these objects. There are three types of System
2 min read
if command in linux with examples
if command in Linux is used for conditional execution in shell scripts.The if command is essential for writing scripts that perform different actions based on different conditions. if COMMANDS list is executed, if its status is true, then the then COMMANDS list is executed. Otherwise, each elif COMM
4 min read
insmod command in Linux with examples
insmod command in Linux systems is used to insert modules into the kernel. Linux is an Operating System that allows the user to load kernel modules on run time to extend the kernel functionalities. LKMs(Loadable Kernel Modules) are usually used to add support for new hardware (as device drivers) and
3 min read
Chaining Commands in Linux
Chaining commands in Linux is a powerful technique that allows you to execute multiple commands sequentially or simultaneously through the terminal. This approach resembles writing short shell scripts that can be directly executed in the terminal, enhancing the efficiency of your workflow. By using
7 min read
function command in Linux with examples
The function is a command in Linux that is used to create functions or methods. It is used to perform a specific task or a set of instructions. It allows users to create shortcuts for lengthy tasks making the command-line experience more efficient and convenient. The function can be created in the u
2 min read