shell scripting tutorial

In this Shell Scripting Tutorial, we are going to learn everything about shell scripting. The need for shell scripting is increasing day by day. Shell scripting languages can be used for multiple purposes such as automating repetitive tasks, backup and maintenance of databases, monitoring a system, creating a programming environment, linking existing programs together, and various other tasks. So, first of all, let us try to understand what exactly a Linux shell is. 

  1. Linux Shell  
  2. Shell Script 
  3. A quick introduction to echo
  4. Using Variables
  5. Command Line Arguments 
  6. Using Arrays  
  7. Basic Operators  
  8. Decision Making 
  9. Shell Loops  
  10. Nested loops  
  11. Infinite Loops
  12. Loop Control
  13. Quoting Mechanisms 
  14. Shell Input-Output Redirections 
  15. Shell Functions 

Linux Shell  

The Linux operating system is a set of programs that acts as a link between the computer and the user. Linux has a kernel which talks to the hardware. But a kernel does not understand the language of a user. And this is where a shell comes into the picture. A shell is an interpreter between the kernel and the user. A Linux shell is a command-line interpreter that provides a command-line user interface for Linux operating systems. It gathers input from the users and executes the programs, and displays the output of a program after executing the same. There are various types of shell present which are listed below – 

  1. Bourne shell (sh)
  2. Korn shell (ksh)
  3. Bourne again shell (bash)
  4. POSIX shell (sh)
  5. C shell (csh)
  6. TENEX/TOPS C shell (tcsh)

Shell Script 

A shell script is a series of commands which are listed in the order of their execution. The operations which can be performed by a shell script include file manipulation, program execution, printing texts, and many more. The users can use Nano, Vim, GNU Emacs, or any other editor which is available for Linux to write or edit the scripts in Linux.

Now let us take a small example and see how a shell script looks like. In this particular example, we have created a shell script named age.sh. The extension of a shell script is “.sh”. The #!/bin/sh line tells the system that the commands in the scripts are going to be executed by the Bourne Shell (bash). Comments can also be added in a script by following a # sign in the script.

The following script uses the read command, which takes the age as an input from the user and assigns it as the value of the variable AGE, and finally prints it on STDOUT. 

#!/bin/sh 
# This script captures the age of a user and prints the same as an output.
Echo “Please enter your age.”
Read AGE
Echo “Your age is , $AGE.”

After saving the file as age.sh in the system we need to make it executable by running the following command.

$chmod +x age.sh

To run or execute a shell script the user is supposed to run the ./script_name command on the terminal. Below is a sample run of the above script – 

$ ./age.sh

Please enter your age.

20

Your age is 20.

This was a very basic and small script. The shell is a real programming language like any other programming language, and it includes various variables, control structures, arrays, and many other functionalities. Now let us try to understand Linux Shell Scripting in detail.

A quick introduction to echo  

The echo command is similar to the common output commands (print, puts, cout) in most programming languages. Let us see few examples of how the echo command works– 

echo “Hello World !”

The output of the above command is Hello World !.

echo “Hello $USER, your current working directory is $PWD.”

The output of the above command is Hello affay07, and your current working directory is /home/affay07. In this example, the variables USER and PWD which we have taken are environment variables. And the output values will differ from system to system. In a shell script, the user can create user-defined variables as well, which we will see below.

Using Variables  

A variable is nothing but a character string to which the user assigns a value. The assigned value to a variable can either be a text or a number or a file name or any other type of data, whichever is required. The shell allows the user to create, modify and delete the variables.

There are a few basic rules when it comes to creating a variable. The variable name can contain only small and capital alphabets (a-z or A-z), numbers (0-9), or underscore characters (_). Other than these characters, no other character can be used to create a variable name—for example, Variable_01, VAR_02, etc. There are mainly three types of variables are present when the shell is running –

  1.  Local Variables
  2. Environment Variables
  3. Shell Variables

How can we define a variable in shell script is shown below – 

Var_name = value

For example,  FRUIT = “Pineapple” 

In the above example, the variable is defined as FRUIT and the value which has been assigned to the FRUIT variable is “Pineapple”.

The value stored in a variable can be accessed by prefixing its name with the dollar ($) sign. For example – 

#!/bin/sh

FRUIT = “Pineapple” 

echo “My favourite fruit is $FRUIT.”

The output of the following script will be as following – 

My favourite fruit is Pineapple.

If a user wants to remove or delete a variable from the list of the variables then the unset command will be used – 

Unset Variable_Name

Special Variables 

There are various special variables available in a shell script that are used for specific functions. A few of them are listed below – 

$$ – This represents the process ID number (PID) of the current shell. 

$0 – This variable represents the file name of the current script.

$# – This variable represents the number of the arguments which are provided to a script.

$n – This variable is related to the arguments with which a script was requested. The character n here is a positive decimal number that represents the position of an argument. For example, the first argument will be $1. The second argument will be $2. So on and so forth.

$* – This variable represents that all the arguments are double quoted. In the case of a script receiving two arguments, S* will be analogous to $1  $2.

$@ – This variable represents that all the arguments are individually double quoted. In the case of a script receiving two arguments, S@ will be analogous to $1  $2.

$! – This variable represents the process number of the last background command.$? – This variable represents the exit status of the command which was executed at last.

Command Line Arguments 

The command-line arguments are positional parameters in a shell script, where $0 represents the actual shell script, command, function, or program and $1, $2, …$9 represents the arguments of that particular command. Various special variables are used in command-line arguments. We can understand the concept well with the help of the following script – 

#!/bin/sh

echo “Script name:  $0”

echo “First favourite fruit : $1”

echo “Second favourite fruit : $2”

echo “Total number of fruits : $#”

Let us have a look at the sample run of the above script – 

$./fruit.sh Pineapple Mango

Script name : ./fruit.sh

First favourite fruit : Pineapple

Second favourite fruit : Mango

Total Number of fruits : 2

Using Arrays  

A shell variable can hold only a single value, and hence they are called scalar variables. But a different kind of variable is also available in shell scripting, which is called an array variable. An array variable is capable of holding multiple values at the same time. With the help of arrays, a grouping of a set of variables can be done very easily. A user can create a single array variable that stores multiple values rather than creating different variables again and again for a particular data type. The naming of an array variable is done the same as a shell variable. 

The array initialization can be done in the following way in a bash shell – 

array_name = (value1, value2 … valuen)

After creating the array variable, we can access the same in the following way – 

${array_name[index]}

Here array_name represents the name of the array and index represents the index of the value that the user wants to access from the array. Let us see a small example of how to define and access an array using the shell script – 

#!/bin/sh

FRUIT[0] = “Pineapple”

FRUIT[1] = “Mango”

FRUIT[2] = “Apple”

FRUIT[3] = “Grapes”

FRUIT[4] = “Banana”

echo “First fruit : ${FRUIT[0]}”

echo “Second fruit : ${FRUIT[1]}” 

The output of the above script execution will be as following – 

$./fruit.sh 

First fruit : “Pineapple”

Second fruit : “Mango”

A user can access all the values in an array with the help of the following command – 

#!/bin/sh

FRUIT[0] = “Pineapple”

FRUIT[1] = “Mango”

FRUIT[2] = “Apple”

FRUIT[3] = “Grapes”

FRUIT[4] = “Banana”

echo “All the fruits present in the array are : ${FRUIT[*]}”

The output of the above script execution will be as following – 

$./fruit.sh 

All the fruits present in the array are: Pineapple Mango Apple Grapes Banana 

Basic Operators  

There are various operators available in a shell script that are supported by a shell. Let us discuss all the operators which are supported by Bourne shell (bash). Bourne shell uses an external program such as awk or expr to perform basic arithmetic operations. The user should write the complete expression inside the backtick (“ ). Let us see a small example of how to add two numbers using shell script –

#!/bin/sh 

add = `expr 5 + 5`

echo “Sum of two numbers is : $add” 

The output of the above script is as following – 

Sum of two numbers is: 10

The types of operators which are available in shell script are as following – 

  1. Arithmetic operators
  2. Boolean operators
  3. Relational operators 

Arithmetic operators –

Let us discuss what all arithmetic operators are supported by bash. Suppose the value of variable x = 200 and variable y = 100.

a.) Addition operator (+) : The addition operator adds the values which are present at both sides of the operator. For example, `expr $x + $y` will give 300 as an output.

b.) Subtraction operator (-) : The subtraction operator subtracts the value of the right hand operand from the value of the left hand operand. For example, `expr $x – $y` will give 100 as an output.

c.) Multiplication operator (*) : The multiplication operator multiplies the values present at both sides of the operator. For example, `expr $x * $y` will give 20000 as an output.

d.) Division operator (/) : The division operator divides the value of the left hand operand by the value of the right hand operand. For example, `expr $x / $y` will give 2 as an output.

e.) Modulus operator (%) : The modulus operator divides the value of the left hand operand by the value of the right hand operand and returns the value of remainder as an output. For example, `expr $x % $y` will give 0 as an output.

f.) Assignment operator (=) : The assignment operator assigns the value of the right operand in the left operand. For example, [ x = $y ] will assign the value of y variable in x variable. The value of x will now become 100.

g.) Equality operator (==) : This operator compares two values and returns true if both are the same and returns false if both are different. For example, [ $x == $y ] will return false as the values of x and y variables are different.

h.) Not Equality operator (!=) : The not equality operator compares two values and returns true if both values are different and return false if both the values are the same. For example, [ $x != $y ] will return true here as the values of x and y variables are different.

Boolean operators –

Let us discuss what all Boolean operators are supported by bash.

a.) Logical AND operator (-a) :  The logical AND operator returns true if the values of both the operands are true.

b.) Logical OR operator (-o) : The logical OR operator returns true if the value of one of the operands is true.

c.) Logical Negation operator (!) : The logical negation operator inverts a false value into true and true value into false.

Relational operators –

Relational operators only work for numeric values. Let us discuss what all relational operators are supported by bash.  

a.) Equal operator (-eq) : Equal operator returns true if the values of both the operands are equal. It is represented as [ $x –eq $y ].

b.) Not Equal operator (-ne) : Not equal operator returns true if the values of both the operands are not equal. It is represented as [ $x –ne $y].

c.) Greater Than operator (-gt) : Greater than operator returns true if the value of the left operand is greater than the value of the right operand. It is represented as [ $x –gt $y ].

d.) Less Than operator (-lt) : Less than operator returns true if the value of the left operand is less than the value of the right operand. It is represented as [ $x –lt $y ].

e.) Greater Than or Equal to operator (-ge) : This operator returns true if the value of the left operand is greater than or equal to the value of the right operand. It is represented as [ $x –ge $y].f.) Less Than or Equal to operator (-le) : This operator returns true if the value of the left operand is less than or equal to the value of the right operand. It is represented as [ $x –le $y ].

Decision Making 

When we are writing a shell script, there may be times when we need to make some decisions based upon the choices that we have. With the help of conditional statements, a user can write multiple conditions in a script and can make the right decision based upon that. Let us see a small example of a conditional statement – 

#!/bin/sh 
read x 
read y
if [[ $x > $y ]]
then
    echo “x is greater than y.”
else
    echo “x is less than y.”
fi

Let us suppose the value of x variable is 20 and the value of y variable is 10. Then the output of the above script will be as following – 

$./compare.sh

20

10

X is greater than y.

There are multiple conditional statements that are available in the Linux shell, which can be used to perform different actions based on the different conditions. Let us now discuss the two decision-making statements which are supported by the Linux shell, which are explained below – 

  1. The if…else statement
  2. The case…esac statement

1. The if…else statements –

The if else statements are widely used decision making statements in shell script which are used to select a particular choice out of various available choices. Below are the multiple forms of if…else statements which are supported by Linux shell – 

a.) if…fi statement 

b.) if…else…fi statement 

c.) if…elif…else…fi statement

2. The case…esac statements –

The case…esac statements used in the shell script are similar to the switch…case statements used in other programming languages. There is only one form of the case…esac statements which is supported by Linux shell which is mentioned below – 

a.) case…esac statement

Shell Loops  

A loop is a very important tool of a programming language that allows the user to execute or run a set of commands on repeat. Let us see a small example of a loop where we will print the alternate values from 1 to 10 using a for loop. 

#!/bin/sh
for n in {1..10..2}
do 
    echo “$n”
done

The output of the above script will be as following – 

1

3

5

7

9

There are different types of loops available in shell programming which are listed below – 

  1. The for loop
  2. The while loop
  3. The select loop 
  4. The until loop

These different loops are used based upon the different requirements that a user has. For example, the for loop is used when the number of iterations is known to the user beforehand. Similarly, the while loop is used to execute the set of commands depending upon the condition which is provided to it. 

Nested loops  

When a loop is put inside another similar or different loop, then they are called nested loops. The concept of nested loops is supported in a shell script. A user can write as many nested loops as required. Let us see an example of nesting while loop, which is mentioned below – 

While condition1 ;
do 
    statement(s)
    while condition2 ;
    do 
        statement(s)
    done
    statement(s)
done

Infinite loops 

The life of a loop depends on the condition which is given to a loop. We come out of a loop once the given condition becomes false in a loop. But there are times when a loop can have an infinite life. This happens when the required condition of a loop does not meet. Hence, a loop that executes for an infinite number of times and does not terminate on any condition, then these types of loops are called infinite loops. Let us see a small example of an infinite loop which is mentioned below – 

#!/bin/sh 
While true 
do 
    echo “This is an infinite loop.”
done

Loop Control  

Till now, we have seen the creation of loops and working with the loops according to certain conditions. While working with the loops, there are times when we need to control a loop based upon the requirements. For example, sometimes, we need to skip a few iterations of a loop, or we need to stop a loop. All these actions can be controlled using the loop control statements.  Below are the loop control statements which are used in shell scripting – 

  1. The break statement
  2. The continue statement 

1. The break statement – A user can use the break statement to terminate the loop immediately. Once the break statement is encountered inside a loop, the execution of the entire loop gets terminated after the execution of all the commands up to the break statement, and the program control resumes at the next statement right after the loop. 

The break statement which is used to come out of a loop is shown below – 

break

To come out from a nested loop the following break command can be used – 

break n

where n represents the nth enclosing loop.

Let us now see an example of how a break statement works inside a loop in a shell script-  

#!/bin/sh
For n in {2..50..2}
do
    echo $n
    if [ $n –eq 10]
    then 
        break
    fi
done

In this example, we are printing the even numbers from 2 to 50. But the break statement will terminate the loop when the n becomes equal to 10. The output of the above script will be as following – 

2

4

6

8

10 

2. The continue statement – The continue statement is somewhat similar to the break statement. But instead of terminating the execution of the entire loop, it terminates only the current iteration of the loop. This statement can be used to execute the next iteration of the loop when an error has occurred somewhere in the script.

The continue statement of a loop is represented as shown below –

continue 

Similar to the break statement, to skip commands from a nested loop, an integer argument can be provided to the continue command which is shown below –  

continue n

where n represents the nth enclosing loop to continue from.

Let us now see an example of how continue statement works inside a loop in a shell script-

#!/bin/sh
for n in {1..10}
do 
    rem = `expr $n % 2`
    if [ $rem –ne 0 ]
    then
        echo “Given number is an odd number.”
        continue
    fi
    echo “This is an even number.”
done

The output of the above script will be as following – 

Given number is an odd number.

This is an even number.

Given number is an odd number.

This is an even number.

Given number is an odd number.

This is an even number.

Given number is an odd number.

This is an even number.

Given number is an odd number.

This is an even number.

Shell Substitution  

On encountering an expression that contains one or more special characters, the shell performs a substitution action. For example – 

#!/bin/sh
fruit = “Pineapple”
echo –e “My favourite fruit is $fruit. \n Because $fruit is very sweet.”

In the above example, the value of the fruit variable is substituted by “Pineapple”. Similarly, the special character “\n” is substituted by a new line. Here, the interpretation of backslash escapes is enabled by the –e option. One can disable the interpretation of the backslash escapes by using –E option. The output of the above script will be as following – 

My favourite fruit is Pineapple.

Because Pineapple is very sweet.

If –e option is not used then the following will be the output of the above script – 

My favourite fruit is Pineapple. \n Because Pineapple is very sweet.

Let us now look at what all the escape sequences can be used in echo command in Linux shell script – 

1.) \a – This character is used as an alert in the echo command.

2.) \b – This character is used as backspace in the echo command.

3.) \c – This character is used to suppress the trailing newline in the echo command.

4.) \f – This character is used as a form feed in the echo command.

5.) \n – As we already saw, this character is used to switch to a new line in the echo command.

6.) \r – This character is used for a carriage return in the echo command.

7.) \t – This character is used as a horizontal tab in the echo command.

8.) \v – This character is used as a vertical tab in the echo command.

9.) \\ – This character is used as a backslash in the echo command.

Command Substitution 

Command substitution is a process using which the outputs of the commands can be substituted or stored inside a variable. To perform the command substitution, the command is written in the back-quote, which is shown below – 

`command`

Let us see the below example of how command substitution is used in shell script – 

#!/bin/sh
current_working_directory = `pwd`
echo “Currently, the user is inside the $current_working_directory directory.”
Date = `date`
Echo “Today’s date is  $date.”

The output of the above script will be as following – 

Currently, the user is inside the home/affay07 directory.

Today’s date is Mon May 24 09:45:32 MST 2021.

Variable Substitution 

Variable substitution is a process using which the value of a variable can be changed based upon its state in the shell programming. Let us look at what all the variable substitution can be used in Linux shell script – 

1.) ${var} – This substitution substitutes the value of the var.

2.) ${var:-word} –In case of var being null or unset, the word is substituted for var in this type of substitution. Also, the value of var remains the same.

3.) ${var:=word} – In case of var being null or unset, var is set to the value of a word in this type of substitution.

4.) ${var:?message} – In case of var being null or unset, a message is printed to a standard error in this kind of substitution. This substitution checks if the variables are set correctly or not.

5.) ${var:+word} – In the case var is set, the word is substituted for var in this type of substitution. Also, the value of var remains the same.

Quoting Mechanisms 

Let us now discuss all the quoting mechanisms which are available in shell scripting.

The Meta characters – The metacharacters are those characters that have special meanings associated with them while being used in a shell script. For example, the $ sign is used to print the value which is assigned to a variable in shell scripting. Below are the special characters (metacharacters) which are used in Linux shell scripting –

$ ^ & * ( ) | \ [ ] < > ‘ “ ; ? \n space tab

To make a character readable in its original form in shell script, it can be quoted by using a quoting mechanism.  There are four forms of quoting available in the shell scripting which are listed below – 

1.) Backslash quoting (\)

2.) Single quote (‘)

3.) Double quote (“)

4.) Back quote (`)

1.) Backslash quoting (\) – A meta character can be quoted by preceding it with a backslash (\). Any special character immediately following the backslash will lose its special meaning and will be used in its original meaning. Let us see a below example of how to quote $ sign to avoid the special meaning associated with it and use it in its original meaning – 

#!/bin/sh 

money = 500 

echo “I have $money dollars in my wallet.”

In this example, the $ sign is used to print the value of the money variable. The output of the above script will be as following – 

I have  500 dollars in my wallet.

Now let us quote the $ sign using backslash and see the output – 

#!/bin/sh 

echo “I have \$500 in my wallet.”

The output of the above script will be as following – 

I have $500 in my wallet.

2.) Single quote (‘) – When quoted in single quotes, all the special characters will lose their special meaning between these single quotes. If there is a large group of special characters present in a single command then instead of quoting each of them with the help of backslashes, the complete command can be quoted using single quotes by putting a single quote at the beginning and at the end of the string. For example – 

echo * $100 (hundred dollars) * 

The above command will be quoted as shown below using the backslash quoting, which looks quite complicated. 

echo \* \$100 \(hundred dollars \) \*

Hence, single quoting can be used to quote a large group of special characters, which is as following –

echo ‘* $100 (hundred dollars) *’ 

3.) Double quote (“) – When quoted in double quotes, most of the special characters will lose their special meaning, a few of them being exceptions. These exceptional characters are $ ` \$ \”  \` and \\. These are exceptions because they are required for the following reasons in shell script – 

1.) $ character – This character is used for parameter substitution in shell scripting.

2.) ` – This character is used for substituting a command in shell scripting.

3.) \$ – This character is used to enable the literal dollar signs in shell scripting.

4.) \” – This character is used to enable the embedded double quotes in shell scripting.

5.) \` – This character is used to enable the literal back quotes in shell scripting.

6.) \\ – This character is used to enable the embedded backslashes in shell scripting.

Let us see a small example of how double quoting works in shell script – 

#!/bin/sh

user = “Affay”

echo ‘Currently, the $user is inside the `pwd` directory.’

The output of the above script will be as following – 

Currently, the $user is inside the `pwd` directory.

As we can see this was not supposed to be the actual output of the above command. Using single quotes prevents variable substitution as well as command substitution. To make variable substitution and command substitution work in the above command we need to do the doubt quoting of the command which is shown below – 

#!/bin/sh

user = “Affay”

echo “Currently, the $user is inside the `pwd` directory.”

The output of the above script will be as following – 

Currently, the Affay is inside the home/affay07 directory.

4.) Back quote (`) – When these special characters are quoted using the back quote then whatever is written between the back quotes will be treated as a command and will get executed. Below is the syntax of how to put any shell command between the back quotes – 

Variable_name = `command`

Let us see a small example of how the back quoting of any shell scripting is done – 

#!/bin/sh

current_working_directory = `pwd`

echo “Currently, the user is inside the $current_working_directory directory.”

The output of the above script will be as following – 

Currently, the user is inside the home/affay07 directory.

Shell Input-Output Redirections 

There are various commands in Linux systems that can be executed on the terminal directly. These commands take input from the terminal as well as return the output to the terminal. The terminal works as standard input as well as standard output by default in a shell script.

Input Redirection – Using input redirection a user can redirect the input of a command from a file. The less than character (<) is used to redirect the input of a command in Linux shell scripting. The syntax which is used for input redirection is shown below – 

$ command < file_name

Output Redirection – Using output redirection a user can redirect the output of a command into a file. The greater than character (>) is used to redirect the output of a command in Linux shell scripting. The syntax which is used for output redirection is shown below – 

$ command > fine_name

Shell Functions 

A shell function is a small logical unit that can be created to perform a specific task similar to a function in any other programming language. A function can be called again and again to perform any task repeatedly. Functions are the best way to reuse a code. A function can be created in a shell script by following the below method – 

func_name () {
    commands
}
Let us create a small function called display – 
#!/bin/sh
Display () {
    echo “My favourite fruit is Pineapple.”
}
Display  # To call the display function.

The output of the above script will be as following – 

My favourite fruit is Pineapple.

Let us now see how a user can pass parameters to a function – 

#!/bin/sh 
Display () {
    echo “My two favourite fruits are $1 and $2.”  # Here $1 and $2 is used to represent the two args.
} 

Display Pineapple Mango # Pineapple and Mango are passed as parameters to the Display function.

The output of the above script will be as following – 

My two favourite fruits are Pineapple and Mango. If we want to return a value from a function then we can use return code syntax for the same. Let us see a small example of how to return a value from a function –

#!/bin/sh 
Display () {
    echo “My two favourite fruits are $1 and $2.”
    return 0
 } 
Display Pineapple Mango

ret=$?    # This is to represent the returned value by function.

echo “The function has returned $ret.”

The output of the above script will be as following – 

My two favourite fruits are Pineapple and Mango.

The function has returned 0.

Shell Man Page Help 

It is a bit difficult for a user to remember all the Linux commands as they are quite large in number as well as have multiple options attached to them. This is why manpage help is available in a shell script to help a user with every step smoothly. Using the man command, one can get the complete details of a Linux command. The syntax for using the man command is shown below –

$man command

For example, if you want to know everything about pwd command then you can take the help of the man command as shown below – 

$man pwd 

Man pages are normally divided into sections. Let us see what all the sections are related to man pages – 

1.) Name

2.) Synopsis

3.) Description 

4.) Options 

5.) See also

6.) Bugs

7.) Examples 

8.) Authors

This was a complete overview of the shell script. Now we have come to the end of this shell scripting tutorial. In this shell scripting tutorial, we have covered the introduction to a Linux shell and shell script followed by the use of variables, special variables, arrays, basic operators, conditional statements, and various loops in a shell script. We have also seen the controlling of shell loops, shell substitutions, shell quoting mechanisms, shell input-output redirections, and shell functions. Last but not least, we have learned how to use the man page help for a smooth experience.

If you wish to upskill in this domain and learn more concepts, check out the various Free Online Courses offered by Great Learning Academy.

Also Read:
Top 95 Shell Scripting Interview Questions & Answers

0

LEAVE A REPLY

Please enter your comment!
Please enter your name here

nineteen − 8 =