Introduction to Bash Scripting

Bash scripting is a powerful tool for system administrators that allows users to automate repetitive tasks, automate workflows, and perform complex operations in a command-line environment. If you’ve never worked with bash scripting before, it might seem daunting at first, but if you are familiar with Linux basics then with a little guidance, you can quickly grasp the fundamentals and start harnessing the full potential of the Bash shell scripting. Let’s start your journey of Bash scripting.

If you interested in Linux Directory Structure

What is Bash Scripting?

Bash, short for “Bourne Again Shell,” is a Unix shell and cli language that provides an interface for users to interact with the Linux operating system. Bash scripting involves writing a series of Linux commands, often referred to as a script, to be executed by the Bash shell. These scripts are essentially small programs that can automate tasks or perform various operations on the system- to reduce task time and manual errors. If you are using a Linux Operating system it is already prebuilt and no third-party installation or setting is required. If you are able to run the command you will able to use bash script.

For example, let’s start with  “Hello, World!” to the terminal:

#!/bin/bash
# This is a simple Bash script
echo “Hello, World!”

Save the above code in a file named hello_world.sh and make this script executable with chmod +x hello_world.sh. Then, run the script using ./hello_world.sh or sh hello_world.sh. The output “Hello, World!” will be displayed on the terminal.

Getting Started with Bash Variables

Variables in any script are used to store data that can be later manipulated or referenced in the script. They are for holding information that changes throughout the script’s execution. Likely a dynamic script it can be defined before use

Below example of using variables to greet a user:

#!/bin/bash
# Example of using variables use in bash script
name=”explinux”
echo “Hello, $name! How are you today?”

Save the above code in a file named greeting.sh and make this script executable with chmod +x greeting.sh. Then, run the script using ./greeting.sh or sh greeting.sh. The output “Hello, explinux! How are you today?” will be displayed on the terminal. Where $name is replaced with the value we have stored in the variable.

Conditional Statements in Bash

Conditional statements allow you to make decisions in our automation script based on given certain conditions like other programming languages. The most commonly used conditional for any script statement in Bash is the if statement which is similar to any other programming language. Only the written part is different we need to tell where the condition ends.

Let’s create a script that checks if a number is positive or negative with the bash script :

#!/bin/bash
# Example of using conditional statements
read -p “Enter a number: ” num

if [ $num -gt 0 ]; then
echo “The number is positive.”
elif [ $num -lt 0 ]; then
echo “The number is negative.”
else
echo “The number is zero.”
fi

Save the above code in a file named positive_negative_check.sh, make it executable, and then run the script as we did in the above examples. Enter a number, and the script will display whether the number is positive, negative, or zero.

Looping in Bash

Loops are used to repeat a set of commands or functions multiple times. In Bash, we have for and while loops to achieve this loop condition.

For example, let’s create a script that prints numbers from 1 to 5 using a for loop:

#!/bin/bash
# Example of using a for loop
for i in {1..5}; do
echo “Number: $i”
done

Save the above code and name it number_loop.sh, make it executable with chmod -x number_loop.sh , and then run the script sh number_loop.sh or ./number_loop.sh. We will see the output with numbers from 1 to 5 printed on the terminal.

Conclusion

Bash scripting is a valuable skill for every Linux admin or Linux automation engineer. With just learning the basics of Bash scripting, we can enhance our productivity, automate tasks, and simplify complex operations on our systems. We covered the essential concepts of Bash scripting, including creating scripts, using variables, implementing conditional statements, and employing loops. With this foundation, you can start playing with Bash scripting. We have a full list or articles on bash scripting check those out to learn more. So, start scripting, and enjoy the power of automation!

- A word from our sposor -

Introduction to Bash Scripting – An Essential Part of Administration