Bash Scripting from Beginner to Advanced: A Step-by-Step Guide

Bash Scripting from Beginner to Advanced: A Step-by-Step Guide


Bash scripting is a powerful way to automate tasks and streamline command-line operations. Whether you’re new to scripting or looking to expand your skills, this guide will take you on a journey from the fundamentals of Bash scripting to more advanced topics, such as file operations, conditional statements, loops, and more. By the end, you’ll have a solid foundation for creating, managing, and enhancing scripts to make your command-line work more efficient and effective

Table of Contents

  1. Introduction to Bash Scripting
  2. Getting Started: Writing Your First Script
  3. Working with Variables
  4. Conditional Statements
  5. Loops in Bash
  6. Creating, Moving, and Deleting Files and Directories
  7. Functions in Bash
  8. User Input in Bash
  9. Error Handling and Debugging
  10. Advanced Topics
  11. Conclusion

1. Introduction to Bash scripting

Bash scripting enables you to write a series of commands in a text file (known as a script) and execute them as a program. This guide will walk you through the basics of Bash scripting, gradually building your knowledge and skills as we move toward more advanced concepts. Whether you’re automating simple tasks or creating complex workflows, you’ll gain the tools needed to use Bash effectively.

2. Getting Started: Writing Your First Script

To write and run your first script:

  1. Open a terminal and create a new file:
   vim hello.sh
Enter fullscreen mode

Exit fullscreen mode

  1. Inside this file, add the following code:
   #!/bin/bash
   echo "Hello, World!"
Enter fullscreen mode

Exit fullscreen mode

  • The line #!/bin/bash(shebang) tells your system to use the Bash interpreter.
  • echo prints text to the terminal.
  1. Save and close the file (:wq).

  2. Give the script executable permissions:
    Before running the script, you need to make it executable:

chmod u+x hello.sh
Enter fullscreen mode

Exit fullscreen mode

  1. Run the script:
    Now, you can run the script by typing:
./hello.sh
Enter fullscreen mode

Exit fullscreen mode

Output:

Hello, World!
Enter fullscreen mode

Exit fullscreen mode

Explanation: Running ./hello.sh executes the script and prints Hello, World!.

3. Working with Variables

Variables in Bash allow you to store data that you can reuse.

#!/bin/bash
name="Joseph"
echo "Hello, $name"
Enter fullscreen mode

Exit fullscreen mode

Output:

Hello, Joseph
Enter fullscreen mode

Exit fullscreen mode

Explanation: Here, we defined a variable name and assigned it a value of "Joseph". When we use $name, Bash substitutes it with “Joseph”. Thus, echo "Hello, $name" outputs Hello, Joseph.

4. Conditional Statements

Conditionals allow you to make decisions in your script.

#!/bin/bash
num=10
if [ $num -gt 5 ]; then
    echo "Number is greater than 5"
else
    echo "Number is 5 or less"
fi
Enter fullscreen mode

Exit fullscreen mode

Output:

Number is greater than 5
Enter fullscreen mode

Exit fullscreen mode

Explanation:

  • The variable num is set to 10.
  • [ $num -gt 5 ] checks if num is greater than 5. Since 10 is greater than 5, the then part executes, printing Number is greater than 5.

5. Loops in Bash

Loops let you repeat actions multiple times.

#!/bin/bash
for i in 1 2 3 4 5; do
    echo "Number: $i"
done
Enter fullscreen mode

Exit fullscreen mode

Output:

Number: 1
Number: 2
Number: 3
Number: 4
Number: 5
Enter fullscreen mode

Exit fullscreen mode

Explanation:

  • This for loop goes through each number from 1 to 5.
  • Each iteration prints Number: $i, where $i is the current number.

6. Creating, Moving, and Deleting Files and Directories

This section covers basic file management tasks in Bash.

Creating Files and Directories

  1. Creating a File:
   #!/bin/bash
   touch newfile.txt
   echo "File 'newfile.txt' created."
Enter fullscreen mode

Exit fullscreen mode

Output:

   File 'newfile.txt' created.
Enter fullscreen mode

Exit fullscreen mode

Explanation: The touch command creates an empty file named newfile.txt. The echo statement confirms the file’s creation.

  1. Creating a Directory:
   #!/bin/bash
   mkdir newfolder
   echo "Directory 'newfolder' created."
Enter fullscreen mode

Exit fullscreen mode

Output:

   Directory 'newfolder' created.
Enter fullscreen mode

Exit fullscreen mode

Explanation: mkdir creates a new directory named newfolder.

Moving Files and Directories

Moving files or directories helps keep things organized.

  1. Moving a File:
   #!/bin/bash
   mv newfile.txt newfolder/
   echo "Moved 'newfile.txt' to 'newfolder/'"
Enter fullscreen mode

Exit fullscreen mode

Output:

   Moved 'newfile.txt' to 'newfolder/'
Enter fullscreen mode

Exit fullscreen mode

Explanation: The mv command moves newfile.txt into newfolder/.

  1. Renaming a File:
   #!/bin/bash
   mv newfolder/newfile.txt newfolder/renamedfile.txt
   echo "Renamed 'newfile.txt' to 'renamedfile.txt'"
Enter fullscreen mode

Exit fullscreen mode

Output:

   Renamed 'newfile.txt' to 'renamedfile.txt'
Enter fullscreen mode

Exit fullscreen mode

Explanation: Here, mv changes the file name from newfile.txt to renamedfile.txt within newfolder.

Copying Files and Directories

Copying creates a duplicate of the file or folder.

  1. Copying a File:
   #!/bin/bash
   cp newfolder/renamedfile.txt copyfile.txt
   echo "Copied 'renamedfile.txt' to 'copyfile.txt'"
Enter fullscreen mode

Exit fullscreen mode

Output:

   Copied 'renamedfile.txt' to 'copyfile.txt'
Enter fullscreen mode

Exit fullscreen mode

Explanation: The cp command copies renamedfile.txt and names it copyfile.txt.

  1. Copying a Directory:
   #!/bin/bash
   cp -r newfolder copiedfolder
   echo "Copied directory 'newfolder' to 'copiedfolder'"
Enter fullscreen mode

Exit fullscreen mode

Output:

   Copied directory 'newfolder' to 'copiedfolder'
Enter fullscreen mode

Exit fullscreen mode

Explanation: cp -r recursively copies newfolder and its contents to a new directory, copiedfolder.

Deleting Files and Directories

Use rm carefully, as deletions are permanent.

  1. Deleting a File:
   #!/bin/bash
   rm copyfile.txt
   echo "Deleted 'copyfile.txt'"
Enter fullscreen mode

Exit fullscreen mode

Output:

   Deleted 'copyfile.txt'
Enter fullscreen mode

Exit fullscreen mode

Explanation: rm removes copyfile.txt.

  1. Deleting a Directory:
   #!/bin/bash
   rm -r copiedfolder
   echo "Deleted directory 'copiedfolder'"
Enter fullscreen mode

Exit fullscreen mode

Output:

   Deleted directory 'copiedfolder'
Enter fullscreen mode

Exit fullscreen mode

Explanation: rm -r deletes the directory and its contents.

7. Functions in Bash

Functions organize code into reusable sections.

#!/bin/bash
greet() {
    echo "Hello, $1!"
}
greet "Joseph"
Enter fullscreen mode

Exit fullscreen mode

Output:

Hello, Joseph!
Enter fullscreen mode

Exit fullscreen mode

Explanation: greet is a function that takes an argument ($1) and prints Hello, Joseph!.

8. User Input in Bash

#!/bin/bash
echo "Enter your name:"
read name
echo "Hello, $name!"
Enter fullscreen mode

Exit fullscreen mode

Output:

Enter your name:
Joseph
Hello, Joseph!
Enter fullscreen mode

Exit fullscreen mode

Explanation: read takes user input, and echo displays the name.

9. Error Handling and Debugging

#!/bin/bash
set -e  
set -x  

echo "This is a test script."
invalid_command  
Enter fullscreen mode

Exit fullscreen mode

Explanation: set -e: Stops the script immediately if any command fails, helping to prevent unintended actions after an error.
set -x: Prints each command before executing it, making it easier to trace the script’s flow for debugging.
The script includes an intentional error invalid_command to show how set -e halts execution upon encountering a problem. Together, set -e and set -x make it easier to identify and troubleshoot issues in the script

10. Advanced Topics in Bash Scripting

Arrays

names=("Joseph" "Alice" "Bob")
echo "First name: ${names[0]}"
Enter fullscreen mode

Exit fullscreen mode

Output:

First name: Joseph
Enter fullscreen mode

Exit fullscreen mode

Associative Arrays

Associative arrays allow you to use named keys instead of numbered indices, making it easier to store key-value pairs.

#!/bin/bash
declare -A student_ages
student_ages["Joseph"]=24
student_ages["Alice"]=22

echo "Joseph's age is: ${student_ages["Joseph"]}"
echo "Alice's age is: ${student_ages["Alice"]}"
Enter fullscreen mode

Exit fullscreen mode

Output:

Joseph's age is: 24
Alice's age is: 22
Enter fullscreen mode

Exit fullscreen mode

Explanation: declare -A initializes an associative array, where we assign ages to specific names. ${student_ages["Joseph"]} retrieves Joseph’s age.

Reading from a File

Bash can read data from a file line-by-line, which is helpful for processing large datasets.

  1. Create a text file named names.txt:
   Joseph
   Alice
   Bob
Enter fullscreen mode

Exit fullscreen mode

  1. Create a script to read each line:
   #!/bin/bash
   while IFS= read -r line; do
       echo "Name: $line"
   done < names.txt
Enter fullscreen mode

Exit fullscreen mode

Output:

Name: Joseph
Name: Alice
Name: Bob
Enter fullscreen mode

Exit fullscreen mode

Explanation: The while loop reads each line in names.txt, storing it in the variable line. IFS= read -r ensures each line is read exactly as it appears.

Writing to a File

You can also write data to a file from within a script.

#!/bin/bash
echo "Creating a list of cities..."
echo "New York" > cities.txt
echo "Los Angeles" >> cities.txt
echo "Chicago" >> cities.txt
echo "Cities have been saved to cities.txt"
Enter fullscreen mode

Exit fullscreen mode

Output:

Creating a list of cities...
Cities have been saved to cities.txt
Enter fullscreen mode

Exit fullscreen mode

Explanation:

  • > creates or overwrites cities.txt with “New York.”
  • >> appends “Los Angeles” and “Chicago” to the file.
  • After running the script, you’ll find the cities in cities.txt.

Running Background Jobs

Background jobs allow a script to run tasks without waiting for them to complete. This is useful for running multiple tasks simultaneously.

#!/bin/bash
sleep 5 &
echo "This message appears immediately, while the background job waits."
wait
echo "Background job has completed."
Enter fullscreen mode

Exit fullscreen mode

Output:

This message appears immediately, while the background job waits.
Background job has completed.
Enter fullscreen mode

Exit fullscreen mode

Explanation: sleep 5 & runs in the background, so “This message appears immediately” is printed right away. The script then waits for the background job to finish with wait.

Using sed for Text Manipulation

sed is a powerful text editor for filtering and transforming text within a file or stream.

#!/bin/bash
echo "Altschool Africa is great!" > school.txt
sed -i 's/great/awesome/' school.txt
cat school.txt
Enter fullscreen mode

Exit fullscreen mode

Output:

Altschool Africa is awesome!
Enter fullscreen mode

Exit fullscreen mode

Explanation: The sed command replaces “great” with “awesome” in school.txt. The -i flag edits the file in place, and cat displays the modified content.

Using awk for Data Processing

awk is useful for handling structured data, such as CSV files. Here’s an example to print the first column from a sample data file.

  1. Create a Data File students.csv:
   Joseph,Cloud Engineering
   Alice,Frontend Development
   Bob,Data Science
Enter fullscreen mode

Exit fullscreen mode

  1. Write the script:
   #!/bin/bash
   awk -F, '{print $1}' students.csv
Enter fullscreen mode

Exit fullscreen mode

Output:

Joseph
Alice
Bob
Enter fullscreen mode

Exit fullscreen mode

Explanation: awk -F, sets the delimiter to a comma, and {print $1} outputs the first column of each line in students.csv.

Using trap for Handling Signals

The trap command helps manage signals, like when you want to stop a script gracefully.

#!/bin/bash
trap 'echo "Exiting..."; exit' SIGINT SIGTERM

echo "Running script. Press CTRL+C to stop."
while :; do
    sleep 1
done
Enter fullscreen mode

Exit fullscreen mode

Output (after pressing CTRL+C):

Running script. Press CTRL+C to stop.
Exiting...
Enter fullscreen mode

Exit fullscreen mode

Explanation: trap catches SIGINT (triggered by CTRL+C) and SIGTERM, printing “Exiting…” before ending the script. Without trap, the script would end abruptly.

11. Conclusion

In this guide, we’ve explored the fundamentals of Bash scripting, from writing simple scripts to mastering more advanced concepts like file management, functions, user input, error handling, and working with arrays. By now, you should be able to confidently automate tasks, create reusable scripts, and efficiently manage system operations with Bash.

Happy Scripting!



Source link
lol

By stp2y

Leave a Reply

Your email address will not be published. Required fields are marked *

No widgets found. Go to Widget page and add the widget in Offcanvas Sidebar Widget Area.