Bash Script

To create a bash script, you can simply create a text file with your desired name with .sh extension. This extension tells the user that this file is a bash script. Before you start creating your bash script, you need to let the system know that this is a bash script. This can be done by using the shebang construct. For example,

#!/bin/bash

This should be the first line in your script.

Make the Script Executable

After you finish formatting your script, you need to save it. Once it’s saved, you need to make it executable to be able to run it. This can be done using the chmod command which is used for the file permission. To make the script executable, you can use the following command:

chmod +x filename

+x is used to allow the user to run the script.

For example, to make the file test.sh executable, type the following.

chmod +x test.sh

Run Bash Scripts

After you make your script, you can run it. To run the script, you need to:

  1. Open a terminal.

  2. Type ./ followed by the filename.

    For example, to run test.sh script, type the following.

./test.sh

Comments

When writing Bash scripts, it’s always a good practice to make your code as clean and understandable. Comments are used to improve the readability of your code. A comment is a human-readable explanation or annotation that’s written in the shell script.

Hash mark (#) is used to write comments in bash scripts. Bash ignores everything written on the line after the #. The only exception to this rule is when the first line of the script starts with the #! characters (shebang).

The Set Builtin

The set command is used to modify the operating parameters of the Shell environment, which means that the environment can be customized. Different parameters can be used with the set command, and you can find on this page point_right official manual for the complete list. In this tutorial, the three most commonly used Set Builtin’s will be discussed.

set -u

When a script is being executed, by default, Bash will ignore the variable that doesn’t exist. Using set -u will report an error instead of continuing execution if the script contains a variable that’s used and not defined.

Example:

#!/bin/bash
set -u

echo "Example of set -u"
echo $a
echo "End"

Output:

Example of set -u
./test2.sh: line 5: a: unbound variable

In this example, an error unbound variable was reported because the variable a isn’t defined.

set -x

By default, after the script is executed, it only displays the results. If multiple commands are executed continuously, the results will be outputted continuously. Sometimes, you may wonder which command produces this piece of the result. Use set -x to output the executed command line before its execution result.

Example:

#!/bin/bash
set -x

echo "Example of set -x"
echo "This is the second command"
echo "End"

Output:

+ echo 'Example of set -x'
Example of set -x
+ echo 'This is the second command'
This is the second command
+ echo End
End

set -e

It can be used to make the script execution end whenever an error occurs.

Example:

#!/bin/bash
set -e
echo "Example of set -x"
cho "This is the second command"
echo "End"

Output:

Example of set -x
./test2.sh: line 4: cho: command not found

Variables

You can define variables in bash scripts. Variables can be defined as follows point_down

var_name=value

For example, to define a variable named HPC with Discovery as it’s value, you can add the following statement to your script.

HPC="Discovery"

To access the value stored in a variable, prefix its name with the dollar sign ($). For example, to print the value of HPC variable, enter the following.

echo $HPC

This prints the value HPC as follows.

Discovery

Conditional Statements

Conditional statements can be used in bash scripts like other programming languages. There are different forms of conditional statements in bash scripts.

if condition

Syntax

if [ condition ]
then
  commands
fi

For example, the following script prints The price is cheap if the value of the PRICE variable is less than 17.

#!/bin/bash
PRICE=15
if [ $PRICE -lt 17  ]
then
  echo "The price is cheap"
fi

if else condition

Syntax

if [ condition ]
then
  commands
else
  commands
fi

The previous example can be extended to print Price is expensive if the value of the PRICE variable isn’t less than 17. It can be written as follows.

#!/bin/bash
PRICE=15
if [ $PRICE -lt 17  ]
then
  echo "The price is cheap"
else
  echo "The price is expensive"
fi

You may need to use more than a condition, you can use the following operators to connect the conditions.

  • -a which represents and operator.

  • -o which represents or operator.

Bash script uses different logical operators in the conditions. The following table shows some of them.

Operator

Description

-le

checks if a numeric value less than or equal another numeric value(⇐)

-lt

checks if a numeric value less than another numeric value (<)

-gt

checks if a numeric value greater than another numeric value (>)

-ge

checks if a numeric value greater than or equal another numeric value (>=)

==

checks if two values are equal

!=

checks if two values aren’t equal

Loops

Loops are useful in bash scripts. Loops allow users to take a series of commands and keep re-running them until a particular situation is reached. They’re useful for automating repetitive tasks.

for loop

It says for each of the items in a given list, perform the given set of commands. It has the following syntax.

for var in <list>
do
<commands>
done

Example:

#!/bin/bash
# Basic for loop
courses="OS PL Algorithms"
for course in $courses
do
echo $course
done

Output:

OS
PL
Algorithms

You can use ranges instead of specifying all elements of the list. For example, to print the even numbers between 1 and 20, you can use the following script.

#!/bin/bash
for i in {1..20}
do
   if [ $((i % 2)) -eq 0 ]
   then
      echo "$i is even"
   fi
done

Output:

2 is even
4 is even
6 is even
8 is even
10 is even
12 is even
14 is even
16 is even
18 is even
20 is even