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 ef
4 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
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
apt-get command in Linux with Examples The command-line tool `apt-get` is the most popular package management tool used in our Debian-based Linux operating system. This article provides an overview of `apt-get` and its basic syntax. It will include the most commonly used commands, their syntax, description, and examples. It also gives an
15+ min read
username Command in Linux With Examples Linux as an operating system holds the capabilities of handling multiple users each with a username and a display name (Full Name). So it is important to keep a check on the users and their related information in order to maintain the integrity and security of the system. Whenever a user is added it
4 min read
aptitude command in Linux with examples The aptitude command in Linux provides a user-friendly interface to interact with the machine's package manager. It functions similarly to a control panel, like in Windows, allowing you to install, upgrade, and remove packages. The command can be used in either a visual interface or directly via the
4 min read
Basic CentOS Linux Commands in linux CentOS is a free and open-source operating system that aims to provide a stable reliable, and community-supported platform for servers and other enterprise applications. In this article, we will be covering CentOS Linux basics commands and functions of CentOS and also we will look into the advanced
4 min read
fc Command in Linux with Examples As we all know that LINUX is command friendly and while working on LINUX, you may deal with very long commands that may include long paths or really difficult syntax, and imagine what if working with such commands you do a minor mistake which will require re-writing of the entire command synopsis an
3 min read