Bash Scripting 101: A Comprehensive Guide to Building Powerful Scripts

Bash Scripting 101: A Comprehensive Guide to Building Powerful Scripts

A beginners guide to bash scripting

Introduction

A common command-line interface and scripting language used in Unix-based operating systems like Linux and macOS is called Bash (Bourne-Again Shell). It is an open-source application that offers a text-based user interface for communicating with the operating system and issuing instructions. Most Linux distributions employ bash by default, frequently used for server development and management as well as scripting and automation chores.

Job control, shell scripting, command-line editing, and built-in facilities for file manipulation, process management, and networking, are just a few of the many commands and features that Bash supports. It is a potent tool for automation and system administration duties because it supports variables, loops, conditionals, and other programming elements.

Writing hello world

As we do in any programming language let's begin with writing hello world in bash and for that, we need to create a file with extension .sh so run the command

nano hello.sh

It will create a hello.sh file and then we need to enter the below statements

#!/bin/bash
echo "Hello World"

In the first line #! is known as shebang, by /bin/bash we are saying that it is a bash shell and this command will print Hello World.

Variables

Creating variables

name="person"

In the above command, name variable is created and a value person is assigned to it.

Accessing variables

echo $name

In the above command, the value of the variable name is printed and we are accessing the value of the variable using $name.

Built-in variables:

  •   $RANDOM
    

    $RANDOM gives a random value between 0 and 32767

  •   $SHELL
    

    $SHELL gives the current shell.

  •   $USER
    

    $USER gives the current user.

  •   $PWD
    

    $PWD gives the current working directory.

  •   $HOSTNAME
    

    $HOSTNAME returns the host.

Taking Input

read variableName

Through the read keyword, we are accepting the user input and we are storing it in the variableName variable.

Taking input from the command

name=$(whoami)

In the above way, we can accept the input in a variable from the command as well.

Mathematical Calculation in bash

In bash, we can perform mathematical calculations within the terminal in the following way

echo $((12+2))

in this way, 14 will be displayed as output in the terminal and we can store the result in the variable as well.

Using if else in bash

    if [[ condition ]];then
        # code to be executed
    elif [[ condition ]];then
       # code to be executed
    else 
       # code to be executed

Using switch case in bash

    case $var in
        1) type="Person1"
        ;;
        2) type="Person2"
        ;;
        3) type="Person3"
        ;;
    esac

Loops in bash

While loop

    while [[ condition ]]
    do
       # code to be executed
    done

Until loop

    until [[ order == "coffee" ]]
    do
        echo "Would you like tea or coffee?"
        read order
    done
    echo "Excellent choice, here is your coffee."

this loop will continue till you give the order as coffee.

For loop

first way

    for i in 1 2 3 4 5;
    do
        echo $i
    done

second way

    for i in {1..10};
    do
        echo $i
    done

third way

    for city in (cat cities.txt);
    do
        weather=$(curl -s http://wttr.in/$city?format=3)
        echo "The weather for $weather"
    done

Conclusion

Bash is a powerful command-line interface and scripting language that is commonly used in Unix-based operating systems like Linux and macOS. It supports job control, shell scripting, command-line editing, and built-in facilities for file manipulation, process management, and networking. Bash is a potent tool for automation and system administration duties because it supports variables, loops, conditionals, and other programming elements.

To write "Hello World" in Bash, we can create a file with the extension .sh and use the echo command. Bash also supports variables, including built-in variables like $RANDOM, $SHELL, $USER, $PWD, and $HOSTNAME. We can take input from the user using the read keyword or accept input in a variable from a command.

In Bash, we can perform mathematical calculations and use if-else and switch-case statements to control program flow. We can also use loops like while, until, and for to repeat a block of code until a certain condition is met.

Overall, Bash is a useful tool for automating and managing tasks in Unix-based operating systems.