Understanding Different Classifications of Shell Commands and Their Usage in Linux

When it comes to gaining absolute control over your Linux system, then nothing comes close to the command line interface (CLI). In order to become a Linux power user, one must understand the different...

When it comes to gaining absolute control over your Linux system, then nothing comes close to the command line interface (CLI). In order to become a Linux power user, one must understand the different types of shell commands and the appropriate ways of using them from the terminal.

In Linux, there are several types of commands, and for a new Linux user, knowing the meaning of different commands enables for efficient and precise usage. Therefore, in this article, we shall walk through the various classifications of shell commands in Linux.

One important thing to note is that the command line interface is different from the shell, it only provides a means for you to access the shell. The shell, which is also programmable then makes it possible to communicate with the kernel using commands.

Different classifications of Linux commands fall under the following classifications:

1. Program Executables (File System Commands)

When you run a command, Linux searches through the directories stored in the $PATH environmental variable from left to right for the executable of that specific command.

2. Linux Aliases

These are user defined commands, they are created using the alias shell built-in command, and contain other shell commands with some options and arguments. The ideas is to basically use new and short names for lengthy commands.

However, the aliases we have created above only work temporarily, when the system is restarted, they will not work after the next boot. You can set permanent aliases in your .bashrc file as shown below.

Set Aliases Permanent in Linux

After adding them, run the command below to active.

$ source ~/.bashrc

3. Linux Shell Reserved Words

In shell programming, words such as if, then, fi, for, while, case, esac, else, until and many others are shell reserved words. As the description implies, they have specialized meaning to the shell.

You can list out all Linux shell keywords using type command as shown:

$ type if then fi for while case esac else until
if is a shell keyword
then is a shell keyword
fi is a shell keyword
for is a shell keyword
while is a shell keyword
case is a shell keyword
esac is a shell keyword
else is a shell keyword
until is a shell keyword

4. Linux Shell Functions

A shell function is a group of commands that are executed collectively within the current shell. Functions help to carry out a specific task in a shell script. The conventional form of writing shell functions in a script is:

function_name() {
command1
command2
…….
}

Alternatively,

function function_name {
command1
command2
…….
}

Let’s take a look at how to write shell functions in a script named shell_functions.sh.

Instead of executing the two commands: sudo apt update and sudo apt dist-upgrade from the command line, we have written a simple shell function to execute the two commands as a single command, upgrade_system within a script.

Save the file and thereafter, make the script executable. Finally run it as below:

$ chmod +x shell_functions.sh
$ ./shell_functions.sh

Linux Shell Functions Script

5. Linux Shell Built-in Commands

These are Linux commands that built into the shell, thus you cannot find them within the file system. They include pwd, cd, bg, alias, history, type, source, read, exit and many others.

You can list or check Linux built-in commands using type command as shown:

$ type pwd
pwd is a shell builtin
$ type cd
cd is a shell builtin
$ type bg
bg is a shell builtin
$ type alias
alias is a shell builtin
$ type history
history is a shell builtin

Learn about some Linux built-in Commands usage:

15 ‘pwd’ Command Examples in Linux

15 ‘cd’ Command Examples in Linux

Learn The Power of Linux ‘history’ Command

Conclusion

As a Linux user, it is always important to know the type of command you are running. I believe, with the precise and simple-to-understand explanation above including a few relevant illustrations, you probably have a good understanding of the various categories of Linux commands.

You can as well get in tough through the comment section below for any questions or supplementary ideas that you would like to offer us.