This Shell scripting tutorial is aimed at covering all the basics and important information about scripting in Linux or Unix-like systems.
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 a 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 the 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.
|In this Shell ScriptingTutorial, I will be discussing the following topics:|
The architecture of a Linux OS helps us understand the anatomy of 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.
|Related Article: What is Linux|
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:
In UNIX, there are 2 main types of Shells. They are:
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:
The Bourne Shell is divided into 4 different sub-categories:
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
|If you want to enrich your career and become a professional in Linux, then Enroll Our ""|
The C Shell is the UNIX enhancement written by Bill Joy at UCB.
types of C Shells are:
|Related Article: Basic Linux Commands|
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, a 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.
This outputs the list of shells supported on your OS.
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.
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.
The first line of any bash script is to add the shebang or hashbang.
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!, an echo command is used. Add the following to the script.
echo "Hello World!"
Now, the script, “hello world. 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 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.
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,
$ ./helloworld.sh or bash helloworld.sh
It prints the "Hello World!" on the command prompt terminal.
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.
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.
|Related Article: Comparision Between Linux vs Unix|
To define a variable, use the syntax below:
Accessing the variables
To access the variables, use the "$" as a prefix to the variable name.
The example below shows a script that performs the following function:
Declares a variable with a value assigned to it.
#!/bin/sh NAME=" ABC " echo $NAME
The above script executes to print,
In order to make the variables static, that is, whose value cannot be altered, the read-only command is used in the shell scripting.
The script below shows an example to change a normal variable to "read-only".
#!/bin/sh NAME="ABC" readonly NAME NAME="DBC"
Here, we are attempting to change the “read-only” variable value from “ABC” to “DBC”
The output generated says:
/bin/sh: NAME: This variable is read only.
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 "read-only" variables.
The example below shows a script that unsets a variable and tries to print its value
#!/bin/sh NAME="Reva" unset NAME echo $NAME
This gives no output.
|Frequently asked Linux Interview Questions|
There are three types of variables on a running script. They are:
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.
#!/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.
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 an example of it.
#!/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:
Since the Linux command prompt looks scary and minimalistic to 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.
This is the handiest 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.
It displays the list of folders present under the directory "applications".
This command is regarded as a change directory (cd). As the name says, it enables the users to change the current working directory.
I say, you are working for a file in Desktop and want to exit to a file in Documents, use cd to switch.
You will now be having Documents as the current working directory.
mv command stands for the move. This command allows the users to move one file from one directory to another.
mv helloworld.sh/Desktop /Documents
This moves the helloworld. sh from Desktop to Documents.
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.
This gives all the information concerning the "cd" command.
This command stands for make directory (mk). It allows the user to create a new directory in the existing path or the said path.
This creates a new directory called new directory in Documents.
This command stands for remove directory (rmdir). It helps in removing the target directory.
This removes the directory called the new directory.
the touch command is used to make files or in other words, create a new file. You can create a new file of any type using the touch command.
This creates a new file in the working directory of "text" format.
This is called the remove command (rm). It is used to remove files just like rmdir is used to remove directories.
This removes the testFile.txt.
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.
locate -i *new*file*08
-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.
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.
It clears the CLI.
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.
|Linux Training||Jun 28 to Jul 13|
|Linux Training||Jul 02 to Jul 17|
|Linux Training||Jul 05 to Jul 20|
|Linux Training||Jul 09 to Jul 24|
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.