Blog

Shell Scripting Tutorial

  • (4.0)
  • | 2942 Ratings


Introduction

This Shell scripting tutorial is aimed at covering all the basics and important information about scripting in Linux or Unix-like system.
Shell scripting is the method of instructing the shell to perform some functions through the shell script.

The Shell script is a computer program or an environment designed to be run by UNIX like Operating System, which can be easily understood and interpreted by the Unix kernel.
Shell scripting allows you to utilize the powerful capabilities of the shell and automate a lot of sequential tasks, which otherwise would require a lot of commands. Scripting is increasingly gaining popularity in today’s world, spanning from networking domain to supercomputers. Once you learn simple bash commands, you can easily learn Python and Perl, as you will be well aware of how Linux works and what it can do.

Gaining expertise in basic administrative commands on command-line Interface (CLI), will help you automate the routine tasks and will help you save more time, whether you are a system administrator, a developer, or a Linux enthusiast.

What is Shell?

The architecture of a Linux OS helps us understand the anatomy of Shell.

shell

The innermost core of the Linux OS is the kernel. The outermost shell of the Linux OS is Shell. The kernel acts as a window for the software programs to recognize and run on the hardware components. While the Shell receives the commands directly from the user and sends it to the kernel for processing and in turn, returns back the response to the user. It wraps inside of the OS and protects it from any external damage directly. Hence, the name Shell.

We run the commands, programs, and shell scripts directly on Shell.

There are different versions/flavors of Shell, just like different versions of Operating Systems. Each version supports its own set of commands and functions.

Shell Prompt

The prompt, "$", is called the command prompt, which is displayed by the Shell. This command prompt is the interface, on which you can write and execute your commands and programs.

The command prompt reads the first word and interprets the command. The Shell reads the command only once you press "Enter".

The command prompt looks something like this:

shell prompt

Inclined to build a profession as LINUX Developer? Enroll Now for Free Demo here!  Linux Training

Types of Shell

In UNIX, there are 2 main types of Shells. They are:

a) Bourne Shell

The Bourne Shell (sh) is the first kind of shell programmed by Stephen R. Bourne in AT&T Bell Labs in the mid-1970s. Hence it is regarded as the primary Unix Shell.

Features of the Bourne Shell:

  1. It is installed as /bin/sh on most Unix Versions and is the choice of a shell for many users for scripting.
  2. It is very compact and has a high speed of execution.
  3. The default prompt for a non-root user is "$"
  4. The default prompt for a root user is "#"
  5. It lacks the interactive features such as recalling the history of commands.
  6. It doesn't support the arithmetic and logical operations.

The Bourne Shell is divided into 4 different sub-categories:

  1. Bourne shell (sh)
  2. Korn shell (ksh)
  3. Bourne Again shell (bash)
  4. POSIX shell (sh)

Out of the 4 subordinates of the sh, the Bourne Again shell (bash) is the re-invented version of the normal sh. It is improvised and is more intuitive to use with better features. It is the new primary shell that most of the programmers use. The local path of the bash is /bin/bash

b) C Shell

The C Shell is the UNIX enhancement written by Bill Joy at UCB.

Features:

  1. It has incorporated interactive uses, such as aliases and history.
  2. Includes the arithmetic functions and the C-like syntax
  3. The command full path is  /bin/csh.
  4. The default prompt of the non-root user is  hostname%
  5. The default prompt name of the root user is  hostname#

types of C Shells are:

  1. C shell (csh)
  2. TENEX/TOPS C shell (tcsh)

Shell Scripting

Shell scripts are basically a set of commands listed in the order of execution. It is a normal program, which when written well, will include the comments (starting with #). The code of the program may have variables, loops, conditions, iterations, etc., which are executed in the order of listing.

Basically, Shell script is a text file containing all the instructions and commands to be executed by the Shell.

Shells supported by your OS

In order to check the Shells supported on your OS, run the following command on your command prompt.

Command

$cat /etc/shells

This outputs the list of shells supported on your OS.

Example Script

Let us understand the scripting through a simple “hello world” example.

We are working here with the bash script.

The first step is to create a file in a location you prefer; say on Desktop. The file can be created using any editor like vim or using file creation command like touch.

Use the following command to create a new sh file.

Command

$touch helloworld.sh

The .sh is the extension that is attached to the file, to tell the system that it is a bash file. It is not compulsory to use this extension. However, it is always a best practice to include it. It basically helps in better identification and distinction of the file.

Now, let us add the code to the file.

Using Shebang

The first line of any bash script is to add the shebang or hashbang.

Command

#! /bin/bash

The "#!" is called hashbang or shebang. It tells the Shell that the script is going to begin.
/bin/bash is the path of the bash located in your OS.

You can replace this with your local path of bash.

To print Hello World!, echo command is used. Add the following to the script.

echo "Hello World!"

Now, the script, “helloworld.sh” looks like this:

#! /bin/bash
echo "Hello World!"

Next, save and exit the script. Use Shift + : + key w + key q to do the same

Generally, the files created using "touch" or "vim" won't have the execution permission. They are only given access to read and write.

Hence, in order to execute the script, you need to change the permission mode of the file. To do this, run the following command.

Command

chmod +x helloworld.sh

chmod is used to change the mode of permission on file. +x adds the execution mode to the file.

Now execute the file to print the string. To execute,

Command

$ ./helloworld.sh
or
bash helloworld.sh

Output

It prints the "Hello World!" on the command prompt terminal.

Shell Variables

Like the general variables in other programming languages, the Shell script also has variables. Variables store data or point to the location of data.

The shell allows for the creation, execution, and deletion of the variables.

Variable Names

The variable name can have only letters (a to z or A to Z), numbers (0 to 9) or the underscore character (_). The wrong character usage in the variable name will cause a syntax error.

By convention, the variable names in UNIX will be in the UPPERCASE.

Defining Variable

To define a variable, use the syntax below:

Syntax

variable_name=variable_value

Example

NAME="ABC"

Accessing the variables

To access the variables, use the "$" as a prefix to the variable name.

Example

The example below shows a script which performs the following function:

Declares a variable with a value assigned to it.

Command

#!/bin/sh
NAME=" ABC "
echo $NAME

Output

The above script executes to print,

ABC

Read-Only Variables

In order to make the variables static, that is, whose value cannot be altered, the read-only command is used in the shell scripting.

Example

The script below shows an example to change a normal variable to "readonly".

Command

#!/bin/sh
NAME="ABC"
readonly NAME
NAME="DBC"

Here, we are attempting to change the “readonly” variable value from “ABC” to “DBC”

Output

The output generated says:

/bin/sh: NAME: This variable is read only.

Unsetting Variables

Unsetting the variable makes the variable to be removed from the list of variables, by the shell. It is almost like deleting the value of the variable, as you will no more be able to access its value.

Unsetting cannot be performed on the "readonly" variables.

Syntax

unset variable_name

Example

The example below shows a script which unsets a variable and tries to print its value

Command

#!/bin/sh
NAME="Reva"
unset NAME
echo $NAME

Output

This gives no output.

Frequently asked Linux Interview Questions

Types of Variables

There are three types of variables on a running script. They are:

  • Local variables: Local variables are present in the current instance of the shell. They are set by command prompt.
  • Environment variables: These are the variables defined by the shell script. Only those required will be defined. They are available to all the child processes of the shell.
  • Shell variables: These are special variables required for the proper functioning of the shell. They can be either local variables or environment variables.

Shell Comments

Shell comments make the code more readable and user-friendly. The scripting best practices include adding comments at short intervals, to explain the code in brisk.

Example

#!/bin/bash
# Author: Reva
# Script follows here:
echo "Hello World"

The code is interpreted by Shell. That is, it is not compiled. The interpreter ignores the comments and executes the commands directly.

Extended Shell Scripts

The shell is a real programming language, which has all the constructs of a normal programming language such as the loops, iterations, control structures, etc.

All the scripts will not be as simple as the ones shown above. Of course, the complexity increases with the advancement and application.

The advanced shell scripts perform some advanced and more intuitive operations, which increase the usability of the code.

The "read" command used below, takes the input from the keyboard and assigns it to the variable on the go.

Below is the example for it.

Command

#!/bin/sh
echo "what is your name?"
read name
echo "How do you do, $name?"
read remark
echo "I am $remark too!"

The execution of the script is shown below:

10 Important Linux Commands

Since Linux command prompt looks scary and minimalistic to the beginners, which is very much unlike Windows or MacOS, where things look brighter and better, this section aims to help you get comfortable with basic Linux commands.

In fact, Linux is a better platform for developers, as it is more easy and robust inbuilt. So, let's get started.

1. ls

This is the most handy command on Linux bash. It gives us instant information about the directory system under the given file.

It is called the list function (ls). It lists all the folders present under the said path/file.

Command:

ls /applications

It displays the list of folders present under directory "applications".

2. cd

This command is regarded as change directory (cd). As the name says, it enables the users to change the current working directory.

If say, you are working for a file in Desktop and want to exit to a file in Documents, use cd to switch.

Command:

cd Documents

You will now be having Documents as the current working directory.

3. mv

mv command stands for move. This command allows the users to move one file from one directory to another.

Command:

mv helloworld.sh/Desktop /Documents

This moves the helloworld.sh from Desktop to Documents.

4. man

The man command refers to manual command. It gives all the information related to a particular command. Basically, it gives metadata of the target command.

Command:

man cd

This gives all the information concerning the "cd" command.

5. mkdir

This command stands for make directory (mk). It allows the user to create a new directory in the existing path or the said path.

Command:

mkdir newDirectory/Documents

This creates a new directory called newDirectory in Documents.

6. rmdir

This command stands for remove directory (rmdir). It helps in removing the target directory.

Command:

rm newDirectory

This removes the directory called newDirectory.

7. touch

touch command is used to make files or in other words, create new file. You can create a new file of any type using touch command.

Command:

touch testFile.txt

This creates a new file in the working directory of "text" format.

8. rm

This is called remove command (rm). It is used to remove files just like rmdir is used to remove directories.

Command:

rm testFile.txt

This removes the testFile.txt.

9. locate

This is also known as the find command. It helps in finding or locating a particular file, whose name and location you are unsure of.

Command:

locate -i *new*file*08

Explanation

-i tell the command prompt to ignore the case of the hint words

* indicates that it will be a wildcard search. It means that the search for the words will be carried out to display everything and anything that contains the search criteria.

10. clear

This command is most friendly while working on Linux CLI. When the command prompt is cluttered with a lot of commands and information, this command helps in clearing off, all the mess, to present a clean command prompt.

Syntax

$clear

It clears the CLI.

Explore Linux Sample Resumes! Download & Edit, Get Noticed by Top Employers!Download Now!

Advantages of Shell Scripting

  1. The syntax used in shell scripting is very simple and easy to implement. The commands are exactly the same as those used directly in the CLI.
  2. Often, writing and executing a shell script is faster than coding and executing in an equivalent coding language.
  3. The program or file selection is easy in shell scripting.
  4. The scripting offers for interactive debugging, logical sequencing, and decision-making linkage for the existing programs.
  5. The absence of compilation of code turns out to be an advantage for moderately small scripts.
  6. The interpretive nature makes it simpler to debug, by writing the debugging code into a script, which can be re-run multiple times to detect and fix the bugs.
  7. Automation of the frequently performed tasks like rolling logs, extracting data, etc., can be performed efficiently.
  8. A sequence of commands can be executed at once through a script.
  9. It is portable, that is, it can be deployed in any UNIX-like systems, without many modifications.

Conclusion

Shell scripting is very useful to perform operations in Linux and other UNIX-like operating systems. It forms one of the powerful ways of programming in Linux. The scripting language is easy and syntax is not heavy on the processor.

Many programmers and tech geeks across the world prefer to work on Linux systems to code, as it offers one of the robust means of coding like shell scripting.



Subscribe For Free Demo

Free Demo for Corporate & Online Trainings.

Sandeep
About The Author

Sandeep is working as a Senior Content Contributor for Mindmajix, one of the world’s leading online learning platforms. With over 5 years of experience in the technology industry, he holds expertise in writing articles on various technologies including AEM, Oracle SOA, Linux, Cybersecurity, and Kubernetes. Follow him on LinkedIn and Twitter.


DMCA.com Protection Status

Close
Close