Understanding Different Classifications of Shell Commands and Their Usage in Linux

[‘

n

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.

n

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.

n

Suggested Read: 5 Interesting Command Line Tips and Tricks in Linux – Part 1

n

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.

n

Different classifications of Linux commands fall under the following classifications:

n

1. Program Executables (File System Commands)

n

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.

n

You can view the directories in the $PATH as follows:

n

$ echo $PATHrnrn/home/aaronkilik/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/gamesrn

n

In the above order, the directory /home/aaronkilik/bin will be searched first followed by /usr/local/sbin and so on, the order is significant in the search process.

n

Examples of file system commands in /usr/bin directory:

n

$ ll /bin/rn

n

Sample Output

n

total 16284rndrwxr-xr-x  2 root root    4096 Jul 31 16:30 ./rndrwxr-xr-x 23 root root    4096 Jul 31 16:29 ../rn-rwxr-xr-x  1 root root    6456 Apr 14 18:53 archdetect*rn-rwxr-xr-x  1 root root 1037440 May 17 16:15 bash*rn-rwxr-xr-x  1 root root  520992 Jan 20  2016 btrfs*rn-rwxr-xr-x  1 root root  249464 Jan 20  2016 btrfs-calc-size*rnlrwxrwxrwx  1 root root       5 Jul 31 16:19 btrfsck -> btrfs*rn-rwxr-xr-x  1 root root  278376 Jan 20  2016 btrfs-convert*rn-rwxr-xr-x  1 root root  249464 Jan 20  2016 btrfs-debug-tree*rn-rwxr-xr-x  1 root root  245368 Jan 20  2016 btrfs-find-root*rn-rwxr-xr-x  1 root root  270136 Jan 20  2016 btrfs-image*rn-rwxr-xr-x  1 root root  249464 Jan 20  2016 btrfs-map-logical*rn-rwxr-xr-x  1 root root  245368 Jan 20  2016 btrfs-select-super*rn-rwxr-xr-x  1 root root  253816 Jan 20  2016 btrfs-show-super*rn-rwxr-xr-x  1 root root  249464 Jan 20  2016 btrfstune*rn-rwxr-xr-x  1 root root  245368 Jan 20  2016 btrfs-zero-log*rn-rwxr-xr-x  1 root root   31288 May 20  2015 bunzip2*rn-rwxr-xr-x  1 root root 1964536 Aug 19  2015 busybox*rn-rwxr-xr-x  1 root root   31288 May 20  2015 bzcat*rnlrwxrwxrwx  1 root root       6 Jul 31 16:19 bzcmp -> bzdiff*rn-rwxr-xr-x  1 root root    2140 May 20  2015 bzdiff*rnlrwxrwxrwx  1 root root       6 Jul 31 16:19 bzegrep -> bzgrep*rn-rwxr-xr-x  1 root root    4877 May 20  2015 bzexe*rnlrwxrwxrwx  1 root root       6 Jul 31 16:19 bzfgrep -> bzgrep*rn-rwxr-xr-x  1 root root    3642 May 20  2015 bzgrep*rn

n

2. Linux Aliases

n

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.

n

Suggested Read: 10 Amazing and Mysterious Uses of (!) Symbol or Operator in Linux Commands

n

The syntax for creating an alias is as follows:

n

$ alias newcommand='command -options'rn

n

To list all aliases on your system, issue the command below:

n

$ alias -prnrnalias alert='notify-send --urgency=low -i "$([ $? = 0 ] && echo terminal || echo error)" "$(history|tail -n1|sed -e '\''s/^\s*[0-9]\+\s*//;s/[;&|]\s*alert$//'\'')"'rnalias egrep='egrep --color=auto'rnalias fgrep='fgrep --color=auto'rnalias grep='grep --color=auto'rnalias l='ls -CF'rnalias la='ls -A'rnalias ll='ls -alF'rnalias ls='ls --color=auto'rn

n

To create a new alias in Linux, go through some below examples.

n

$ alias update='sudo apt update'rn$ alias upgrade='sudo apt dist-upgrade'rn$ alias -p | grep 'up'rn

n

Create Aliases in Linux
Create Aliases in Linux

n

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.

n

Set Aliases Permanent in Linux
Set Aliases Permanent in Linux

n

After adding them, run the command below to active.

n

$ source ~/.bashrcrn

n

3. Linux Shell Reserved Words

n

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.

n

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

n

$ type if then fi for while case esac else untilrnif is a shell keywordrnthen is a shell keywordrnfi is a shell keywordrnfor is a shell keywordrnwhile is a shell keywordrncase is a shell keywordrnesac is a shell keywordrnelse is a shell keywordrnuntil is a shell keywordrn

n

Suggested Read: 10 Useful Linux Chaining Operators with Practical Examples

n

4. Linux Shell Functions

n

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:

n

function_name() {rncommand1rncommand2rn…….rn}rn

n

Alternatively,

n

function function_name {rncommand1rncommand2rn…….rn}rn

n

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

n

#!/bin/bash rnrn#write a shell function to update and upgrade installed packages rnupgrade_system(){rn        sudo apt update;rn        sudo apt dist-upgrade;rn}rnrn#execute functionrnupgrade_systemrn

n

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.

n

Suggested Read: 5 Shell Scripts for Linux Newbies to Learn Shell Programming

n

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

n

$ chmod +x shell_functions.shrn$ ./shell_functions.shrn

n

Linux Shell Functions Script
Linux Shell Functions Script

n

5. Linux Shell Built-in Commands

n

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.

n

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

n

$ type pwdrnpwd is a shell builtinrn$ type cdrncd is a shell builtinrn$ type bgrnbg is a shell builtinrn$ type aliasrnalias is a shell builtinrn$ type historyrnhistory is a shell builtinrn

n

Learn about some Linux built-in Commands usage:

n

    n

  1. 15 ‘pwd’ Command Examples in Linux
  2. n

  3. 15 ‘cd’ Command Examples in Linux
  4. n

  5. Learn The Power of Linux ‘history’ Command
  6. n

n

Conclusion

n

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.

n

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.

n

‘]