Looking at shells, internal and External commands

Linux's Internal and External Shell commands

Linux’s Internal and External Shell commands

This Study area follows on from this Post , Using Linux Shells

Each of the available command shells in Linux has two primary types of commands, Internal or External.

Internal

By saying a command is Internal, these commands are fully incorporated with in the coding of the shell itself. Each Shell such as (BASH, DASH, KSH and ZSH) offer very similar commands but some also offer unique commands.

You can see a list of a shells internal commands by using the man pages that relate to the shell, i.e. “man bash” or “man ksh”. The text that relates to the internal command set is located under the heading of “Built-in commands”.

The Shell BASH uses the “help” command to show you details that relate to its internal command set:

Bash Help page "$ help"

Bash Help page “$ help”

External

These commands are installed within your Linux Installation, externally of a shell’s own coding.

The main reason for installing external shell commands is to add flexibility to a Linux installation and to allow for command compatibility with existing software.

It should also be noted that external commands are also shared between different shells thus removing object redundancy, the ideal is that only commands that reflect on core differences and performance between shells should be internal to the shell.

The removal of object redundancy both saves disk space and the chance of programming errors.

Determining a commands type

You can use many different methods to determine if a command is internal or external to the shells core, starting with the most effective method as follows :

The type command (Internal and External)

e.g.

$ type pwd
pwd is a shell builtin

the “type” command will return “command(x) is a shell command” if the given command is built into the shell and thus an internal command !

This answer however is not usually enough to help you know the full picture relating to any given command.

It is not unusual that a shell command exist in both internal and external forms, this can be because a (newer, more informative or more compatible) version has been installed into your core system.

To show just how many versions of a single command exist you can add the “-a” option as follows :

$ type -a pwd
pwd is a shell builtin
pwd is /bin/pwd

This use of the type command returns two lines, thus reporting that the command has found two existences of the pwd command.

Other methods of finding a command type (Internal or External) is to use the “which” command or the “whereis” command , these commands will however only confirm if a command is external and if so show its location.

You can also check the reference to “Built-in commands” within the selected shells man pages.

As said above bash also uses the “help” command to list all internal commands and there command line options, if your needed command is not listed here yet exists on the system then it is an external command usually located within your users default path.


Command order of execution and the $PATH environmental variable

When a command exists as both an internal and external command , it is the internal command that takes precedence, if you need to call the external command you need to include its full path when naming it on the command line or in a program or script.

i.e.

>$ type -a time
time is a shell keyword
time is /usr/bin/time

>$ time , # entered on the command line will call the shell’s internal command

>$ /usr/bin/time # explicitly names time as /usr/bin/time

When a command is only installed as an external command and it is called from the command line or in a program/script, it is the users PATH that is searched to locate the external command. Clearly this then requires that the location of the external command is added into your user $PATH environmental variable, it is this variable that is used during any of your operations to find objects you call for execution or files that your operations require.

you can check if an objects location is in your $PATH as follows

>$ which vim
/usr/bin/vim

>$ echo $PATH
/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games

from the above you can see that the text editor “vim” exists in /usr/bin/ and that you $PATH env variable does have this folder location added correctly to it.

NB: You can add a directory path to $PATH variable.Run the below commands on terminal to add a directory path to the PATH-Variable,

su root
PATH=$PATH:/path/to/the/directory

To add the path permanently to the BASH shell environment, add the following line at the end of ~/.bashrcfile

sudo gedit ~/.bashrc

Add the below line at the end,

export PATH=$PATH:/path/to/the/directory

Advertisements
This entry was posted in Linux, Operating systems and tagged , , , , , , . Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s