what are linux commands and the 4 types of them

In Linux or any unix based system, a command refers to a directive that can used to invoke computer program. This program then acts as an interpreter to the command and its options and arguments, and performs a specific task. In most scenarios, a command refers to a directive in the command line which is usually a shell such as bash, csh, ksh etc.

Some of the commands that can used in command line shell are ls, cd, cat etc. As mentioned, some of these command can take arguments which determines which resource the program will act on. Many if not all of the commands also support command line options that modifies the action performed on the resource. Some examples of the command are

$ ls -lisa /home/tom/photos/

In the above command, /home/tom/photos/ is the command line argument, while -l, -i, -s and -a are the command line options. ls is the command and it performs the action of listing or printing out the files and folders in the specified folder.

As you might well know, there are hundreds of thousands of different commands and programs that you can use from the command line. All of these commands can broadly be classified into four different types. Each of them has specific characteristics and priority in an linux/unix environment.

So, given a command how can you identify which type it is? You can use the type command to find what type a particular command is. The type will print out a detailed description of the command type.

Built-in Shell Commands

Depending on the command shell that you are using, there are several different commands that are built into the shell. Some of these commands might be slightly different depending on the shell, but most of the common ones are available on most shells with similar options. These are also called shell builtins.

As the name indicates, these commands are executed right within the shell. This means that it is very fast as there is no external program loading that has to done. The shell builtins are usually simple and trivial tasks that are somehow inherent to the shell, such as printing folder contents.

shell commands that are builtin to the shell

Some of the examples of commonly used shell builtins are cd, bg, jobs, kill, local, logout, echo etc. Shell Builtins take precedence over external or executable programs that are described below.

Shell Functions

These broadly refers to the shell scripts that are incorporated into the linux environment or shell. Shell functions are equivalent to methods, functions or subroutines in other programming languages. These are good ways to reuse code to perform repetitive tasks in shell scripts.

Some shell reserved keywords such while, else, do, case etc that are used to write shell scripts fall into this category.

Command Alias

These are aliases that you can create in your shell for easy execution of other commands. These types of commands or aliases take precedence over all other types of commands.

linux shell aliases example

Executable Programs

These are probably the most commonly used type of commands. These are all the programs that are usually compiled and installed on the machine, such as those you find in /usr/bin/ or /usr/local/bin/. These can also refer to programs or scripts that are written interpretive languages such python, ruby or perl etc.

Some of the examples of this type of commands are zip, wget, vlc, mplayer, and vi. You can easily startup these applications by typing the command in the command line as long as they are in the $PATH variable. You can also execute them by using the absolute path to the executable command.

 

Although rare, it is quite possible that you have different commands that share the same name. When you execute such commands, then a predetermined precedence is used to determine which of the command is executed. The command aliases you have specified will take the highest precedence. The next priority is the shell functions and shell reserved keywords. The shell builtins take the next precedence followed the executable programs.

If there are multiple executables by the same name in the path, then the first one is executed. You can determine the precedence and location of the command that will be executed by using the which command.

Sometimes, you need a quick and easy way to override the precedence and execute a different type of command by the same name. There are different ways of doing this depending on what you want to execute.

In order to execute a builtin command, you can use the shell builtin by the same name, builtin which executes the specified shell builtin. So, an example would be

$ builtin <commandname>

Another useful shell builtin is the command. Using command will allow you to bypass the normal shell lookup. This will ignore the functions and aliases by the same name.

$ command <commandname>

Using a backslash in front of the command name will allow you to bypass any aliases that have been set on the shell.

$ \<commandname>

If there are multiple executables by the same name, and you want to bypass the order in the $PATH variable then you should use the absolute path to program. This will ensure that the application or program that is executed is exactly the one you want.