A gentle introduction to shell scripting in Linux

A gentle introduction to shell scripting in Linux

Bash scripting in Linux

You may hear the term shell scripting many times. But do you know what is a shell and what is shell scripting? In this article, we will learn the basics of shell scripting. Let's start by understanding what is linux kernel.

Linux Kernel

We all know that a computer must have an operating system (OS) to operate. OS consists of different software, and combining them makes a complete system to operate a computer.

A kernel is a piece of software that acts as the core component of an OS. If we compare an OS to a car then the kernel is the engine of the OS.

What is a shell?

A shell is a software program that acts like a layer( i.e., a shell) on the top of the kernel (the engine of the OS). It interprets the user commands, communicates with the kernel and helps to execute the user commands.

What is shell scripting or Bash scripting?

Shell scripting is the process to write a set of commands to interact with the shell. We can write a set of Linux commands for a specific task and save them in a file with _.sh extension, then the file will be called a shell script.

There are different languages and syntaxes for shell scripting, used for different purposes accordingly. For example Bash, Xonsh etc.

  • When Bash syntax is used the script is called a Bash script. Bash stands for Bourne Again Shell, it is the improved version of the sh (original Bourne shell).

  • When Xonsh language (python-based syntax) is used then the shell is called a Xonsh shell or a python shell.

  • Some other examples of shells are csh(c shell), tcsh(turbo c shell) etc.

Keep in mind before starting shell scripting

  1. Shell scripting languages are interpreted languages (i.e. interpreted line by line) interpreted by the respective shell. For example, Bash is a shell scripting language that is interpreted by the Bash shell.

  2. A shell script always starts with #! which is called a shebang or hashbang. The hashbang is used to specify which interpreter will be used to execute the script

Example

#!/bin/sh

echo "Hello World!"

In the above example: #!/bin/sh means, Bourne shell (sh) will be used as the interpreter. It is not mandatory to use the hashbang to specify the interpreter. But if we don't use the shebang, then the default shell interpreter will be used to execute the script. Bash (Bourne Again Shell) is the default shell in most Linux-based operating systems including Ubuntu. So if we don't specify the hashbang then by default #!/bin/bash will be used as the hashbang.

A real example of shell scripting.

To understand shell scripting better let's take an example. Let's say we want to check whether a given number is positive or negative. To solve this, we can write a shell script as shown below

#!/bin/bash

num=$1 # '$1' means take the first argument given with the script

if [ $num -gt 0 ] # '-gt' means greater than

then #if the given number is greater than zero, do the following

echo "$num is a positive number."

else

echo "$num is a negative number."

fi #end of the if-else block

We can save this as a script file let's say check_positivity.sh To execute the file we need to give executable permission as chmod +x check_positivity.sh Then run the file as ./check_positivity.sh 3 , it will show the result as

3 is a positive number.

If we run the script as ./check_positivity.sh -10 it will show the result as

-10 is a negative number.

So using this simple example we learned how to take an input argument, how to use an if-else block and how to print a variable using echo command.