Shell Scripting Guide

Introduction

In this guide we are going to learn how to use BASH shell scripting. Shell scripting is usually used for tasks such as file manipulation, program execution and task automation. Shell scripts contain the same commands you normally run in a terminal, it just allows you to group them together and run them in sequence without having to run each CLI command separately. In this guide I will use nano text editor and Ubuntu 18.04, though you may use any other Linux operating system and an editor of your choice.

 

Create a shell script

Create a new file with the following content:

#!/bin/bash
echo "Hello World"

 

You can use nano my_script.sh  command to create a new file ( i named it my_script.sh) and edit it via nano.

This is a very simple script which just prints ‘Hello world’ to the output. The first line of the script #!/bin/bash is very important, it should contain the full path to the shell which will run the commands. In our case this is BASH shell which is the most popular.

Now  it is time to run our script and see that it works, but before we can do it we should make the file executable, that is make it more like a program than just a plain text file.

We need to run chmod 755 my_script.sh to make it executable:

orkhans@matrix:~$ chmod 755 my_script.sh 
orkhans@matrix:~$ ll my_script.sh 
-rwxr-xr-x 1 orkhans orkhans 31 Jul 14 16:42 my_script.sh*

Now you can run the file and see the output it produces:

orkhans@matrix:~$ ./my_script.sh 
Hello World

 

Environment variables

In our previous example we just printed out static string, but echo command allows us to print environment variables’ values as well.

The following example prints out the value of USER variable. Please, note that when we refer to a variable in a command we have to put a dollar sign before its name : $USER.

Besides that, it is common to refer to a variable in a string by using additional curly braces – ${USER}. It is not necessary in our case, but there are situations when they are required, therefore it is better to get into habit of using them.

Modify the content of the file so it looks like this:

#!/bin/bash

# This line is a comment. Comments usually contain notes about the code
# Greeting the world
echo "Hello World"
echo  "Your username is: ${USER}"
echo $USER

Now run the script. In my case I get the following output, because my user name is orkhans:

orkhans@matrix:~$ ./my_script.sh 
Hello World
Your username is: orkhans
orkhans

We can create our own variables and use them in our code. In the following example we create a variable TODAY and assign it the output of date command, then we print the value of TODAY variable.

Modify the my_script.sh so it looks like this:

#!/bin/bash

# This line is a comment. Comments usually contain notes about the code
# Greeting the world
echo "Hello World"
echo  "Your username is: ${USER}"
echo $USER
TODAY=$(date)
echo $TODAY

 

The output it produces is:

orkhans@matrix:~$ ./my_script.sh 
Hello World
Your username is: orkhans
orkhans
Mon Jul 16 21:37:30 +04 2018

Please, note that date command is used like this $(date), because we want to use the output of this command  and assign it to a variable.

 

Reading a user input

Linux shell allows us to read input from a user and assign it to a variable. The command to read user input is read. Below is a script which reads input from a user and then prints it back.

#!/bin/bash

echo "Please, enter a number:"
read number
echo "You entered ${number}"

 

Run this script and you will get a similar result:

orkhans@matrix:~$ ./my_script.sh 
Please, enter a number:
34
You entered 34

 

Shell script arguments

We have already learnt how to use read command to get input from a user, but there’s also another way of doing this – arguments. Arguments are typed after the script name when you run run script.

Below is a script which takes two parameters and prints them back:

#!/bin/bash

echo "The first argument is: ${1}"
echo "The second argument is: ${2}"

 

The built-in variables ${1}, ${2}${9} contain values of passed arguments and we can refer to them in our script.

Another useful variables are:

$0 – contains script name

$@  contains all arguments in one variable

$$ – contains process ID of a current shell

 

The following example shows passing two arguments arg1 and arg2 to our script:

orkhans@matrix:~$ ./my_script.sh arg1 arg2
The first argument is: arg1
The second argument is: arg2

 

Loops

Loops are special flow control statements which allow a code to be executed until either a certain condition is met or infinitely. There are several types of loop statements, but we are going to look at so called “for” loops.

The example below shows how to go through the items in a list and execute a code for each item.

The variable $@ contains the list of arguments provided to the script and we use “for” loop to print each argument without using multiple echo commands:

#!/bin/bash

for var in $@
do
  echo "${var}"
done

echo "Finished"

 

The code to be repeatedly executed goes between do and done. Variable var is assigned a value from the list ($@) every loop iteration.

Below is the output our script produces:

orkhans@matrix:~$ ./my_script.sh arg1 arg2 arg3 arg4 arg5
arg1
arg2
arg3
arg4
arg5
Finished

 

Comments

You may have noticed these line starting with # in our shell script:

# This line is a comment. Comments usually contain notes about the code
# Greeting the world

These are the comments which are usually used to describe the code. Comments are not interpreted by shell and are useful only for those who are going to read the script. It is very important to write comments, especially when developing a complex script.

 

Add a Comment