Skip to content

Scripting with Bash

Bash scripting allows you to automate tasks, manage system operations, and create custom utilities. This section covers the basics of writing and running Bash scripts.


Basics of Bash Scripting

Shebang

The first line of a script should specify the interpreter.

#!/bin/bash

This line is called a shebang and tells the system to use the Bash shell to interpret the script.

Permissions

Before a Bash script can be run, it needs to be made executable. You can change the permissions of the script file with the chmod command:

chmod +x scriptname.sh

Running a Script

To run a Bash script, you can either call it directly if it's in your PATH, or you can specify the path to the script. For example:

./scriptname.sh

This command runs a script named scriptname.sh in the current directory.

Basic Commands

Bash scripts are written using a series of shell commands. Here is an example of a simple script:

#!/bin/bash
echo "Hi, I'm from CleSucre world!"
date

This script outputs "Hi, I'm from CleSucre world!" and then displays the current date and time as follows:

basic_commands.png

Variables

Variables in Bash are defined without a prefix, and they are accessed using the $ symbol. For example:

#!/bin/bash
name="Visitor"
echo "Welcome, $name!"

variables.png

Conditionals

Bash uses conditional statements to make decisions. The basic syntax for an if statement is:

if [ condition ]; then
  commands
fi

For example:

#!/bin/bash
read -p "Enter your age: " age
if [ $age -ge 100 ]; then
  echo "Wow ! You have my respect sir!"
elif [ $age -ge 18 ]; then
  echo "You are an adult!"
elif [ $age -le 0 ]; then
  echo "Wait... What ?"
elif [ $age -le 1 ]; then
  echo "Are you a baby ?"
else
  echo "You are a child!"
fi

conditionals.png

read -p is used to prompt the user for input.

-ge is a comparison operator that checks if the age is greater than or equal to 18.

-le is a comparison operator that checks if the age is less than or equal to 0.

Loops

Bash supports several types of loops, including for, while, and until. Here is an example of a for loop:

#!/bin/bash
for i in {1..5}; do
  echo "Looping ... number $i"
done

{1..5} is a brace expansion that generates a sequence of numbers from 1 to 5.

loops.png

Functions

Functions in Bash are defined using the function keyword or by simply naming the function:

function function_name {
  echo "First parameter is: $1"
}

You can then call this function with an argument:

for i in {1..5}; do
  function_name $i
done

function_name is the name of the function.

$1 refers to the first argument passed to the function.

$i is the loop variable.

function_name $i calls the function with the loop variable as an argument.

functions.png

Debugging

To debug a Bash script, you can add -x to the shebang line which enables a trace of each command.

Let's try it on the previous script:

#!/bin/bash -x

function function_name {
  echo "First parameter is: $1"
}

for i in {1..5}; do
  function_name $i
done

This will show you each command as it gets executed, along with its arguments and expanded values:

debugging.png

Next Step

Now that you know how to write and run Bash scripts, continue to practice and explore advanced scripting techniques to further enhance your skills.

Previous