- Internal and External Commands in Linux
- They are grouped into two categories:
- Getting the list of Internal Commands
- How to find out whether a command is internal or external?
- Internal vs External
- Internal command in linux
- Pages
- Monday, March 26, 2012
- Internal vs External commands
- Internal and External Commands in Linux
- What is Linux Internal And External Command
- Table of Contents
- Difference Between Linux Internal And External Command
- How To Identify Internal Or External Command
- What is builtin Linux/Unix command?
- How to check PATH variable value ?
- Conclusion
Internal and External Commands in Linux
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 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 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 this works. This program starts running as soon the user log in, and dies when the user log 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 command 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
//use of external command expr
for addition//
$c=`expr $a+$b`
In such a case, use of let will be more better option as it is a shell built-in command so will work faster than the expr which is an external command.
Internal command in linux
Pages
Monday, March 26, 2012
Internal vs External commands
Let us look at these in detail
Internal commands are something which is built into the shell. For the shell built in commands, the execution speed is really high. It is because no process needs to be spawned for executing it. For example, when using the «cd» command, no process is created. The current directory simply gets changed on executing it.
External commands are not built into the shell. These are executables present in a separate file. When an external command has to be executed, a new process has to be spawned and the command gets executed. For example, when you execute the «cat» command, which usually is at /usr/bin, the executable /usr/bin/cat gets executed.
You can get only if you are in bash shell. Bash shell has a command called «help» which will list out all the built-in shell commands.
$ help alias [-p] [name[=value] . ] bg [job_spec . ] bind [-lpvsPVS] [-m keymap] [-f fi break [n] builtin [shell-builtin [arg . ]] caller [EXPR] case WORD in [PATTERN [| PATTERN]. cd [-L|-P] [dir] command [-pVv] command [arg . ] compgen [-abcdefgjksuv] [-o option .
$ type cd cd is a shell builtin
For the internal commands, the type command will clearly say its shell built-in, however for the external commands, it gives the path of the command from where it is executed.
Internal vs External?
The question whether should we use an internal command or an external command OR which is better always does not make sense. Because in most of the situations you will end up using the command which does your job which could be either internal or external.
The big difference in internal vs external command is performance. Internal command are much much faster compared to external for the simple reason that no process needs to be spawned for an internal command since it is all built-into the shell. So, as the size of a script gets bigger, using external commands a lot does adds to its performance.
Not always we get a choice to choose an internal over an external command. However, a careful look at our scripting practices, we might find quite a few places where we can avoid external commands.
Example:
Say to add 2 numbers say x & y:
let is a shell built-in command, whereas expr is an external command. Using expr will be slower. This might be very negligible when you are using it at an one-off instance. Using it in a place say on every record of a file containing million records does give a different dimension to it.
Internal and External Commands in Linux
- 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.
$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
//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.
What is Linux Internal And External Command
Do you know when you run the Linux / Unix command it could be Internal or External ? In this article, we will learn about Linux Internal and External command. For identifying and understand on command type, here we have article for you.
Linux/Unix has so many commands. If you know these commands, your Linux work will be much more easier and faster.
Table of Contents
In this post we have tried to provide the difference between Internal and External command in a simple manner.
Difference Between Linux Internal And External Command
In Linux, generally commands are categorised into two types – Internal Command and External Command.
We have simplify the difference between the two types of command in given below table.
Internal Command | External Command |
---|---|
Internal commands are shell built-in commands. | External commands are other than shell built-in command. |
It doesn’t need to be set in PATH variable. | We have to set the PATH variable for External commands. |
Do not require any process to spawn on execution. | It require process to be spawned when it get executed. |
Example: cd, source, fg, echo | Example: ls,cat. |
Difference between Internal and External Command
How To Identify Internal Or External Command
In above section, we read about the difference between Internal and External Command. But as a user, how can we identify the command type.
For identifying the Internal and External command, we will use the command called type .
We have already written about type command in detail, you must be interested to read this post – “type command : display information about command type on Linux“
For a quick check, use this Syntax
type [argument] [command name]
For example, let’s check if cd command is Internal or External
user1@server01:~$ type cd cd is a shell builtin user1@server01:~$
The above output shows that cd command is Internal Command means Shell Built-in . Try the same type command to identify other commands.
What is builtin Linux/Unix command?
The builtin or shell builtin or Internal command , all have same meaning. The builtin commands are the command which are available in Shell by default.
How to check PATH variable value ?
To check the PATH variable value , run the command echo $PATH in current user shell means when you run the command in terminal it shows the PATH value for current login user in terminal.
Conclusion
In this article, we have described the difference between Linux/Unix Internal and External command. This article will help to enhance the knowledge on Linux Command Line system.