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
Introduction to Bash Scripting
Getting Started: Writing Your First Script
Working with Variables
Conditional Statements
Loops in Bash
Creating, Moving, and Deleting Files and Directories
Functions in Bash
User Input in Bash
Error Handling and Debugging
Advanced Topics
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:
Open a terminal and create a new file:
Inside this file, add the following code:
echo “Hello, World!”
The line #!/bin/bash(shebang) tells your system to use the Bash interpreter.
echo prints text to the terminal.
Save and close the file (:wq).
Give the script executable permissions:
Before running the script, you need to make it executable:
Run the script:
Now, you can run the script by typing:
Output:
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.
name=“Joseph”
echo “Hello, $name“
Output:
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.
num=10
if [ $num -gt 5 ]; then
echo “Number is greater than 5”
else
echo “Number is 5 or less”
fi
Output:
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.
for i in 1 2 3 4 5; do
echo “Number: $i“
done
Output:
Number: 2
Number: 3
Number: 4
Number: 5
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
Creating a File:
touch newfile.txt
echo “File ‘newfile.txt’ created.”
Output:
Explanation: The touch command creates an empty file named newfile.txt. The echo statement confirms the file’s creation.
Creating a Directory:
mkdir newfolder
echo “Directory ‘newfolder’ created.”
Output:
Explanation: mkdir creates a new directory named newfolder.
Moving Files and Directories
Moving files or directories helps keep things organized.
Moving a File:
mv newfile.txt newfolder/
echo “Moved ‘newfile.txt’ to ‘newfolder/'”
Output:
Explanation: The mv command moves newfile.txt into newfolder/.
Renaming a File:
mv newfolder/newfile.txt newfolder/renamedfile.txt
echo “Renamed ‘newfile.txt’ to ‘renamedfile.txt'”
Output:
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.
Copying a File:
cp newfolder/renamedfile.txt copyfile.txt
echo “Copied ‘renamedfile.txt’ to ‘copyfile.txt'”
Output:
Explanation: The cp command copies renamedfile.txt and names it copyfile.txt.
Copying a Directory:
cp -r newfolder copiedfolder
echo “Copied directory ‘newfolder’ to ‘copiedfolder'”
Output:
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.
Deleting a File:
rm copyfile.txt
echo “Deleted ‘copyfile.txt'”
Output:
Explanation: rm removes copyfile.txt.
Deleting a Directory:
rm -r copiedfolder
echo “Deleted directory ‘copiedfolder'”
Output:
Explanation: rm -r deletes the directory and its contents.
7. Functions in Bash
Functions organize code into reusable sections.
greet() {
echo “Hello, $1!”
}
greet “Joseph”
Output:
Explanation: greet is a function that takes an argument ($1) and prints Hello, Joseph!.
8. User Input in Bash
echo “Enter your name:”
read name
echo “Hello, $name!”
Output:
Joseph
Hello, Joseph!
Explanation: read takes user input, and echo displays the name.
9. Error Handling and Debugging
set -e
set -x
echo “This is a test script.”
invalid_command
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
echo “First name: ${names[0]}“
Output:
Associative Arrays
Associative arrays allow you to use named keys instead of numbered indices, making it easier to store key-value pairs.
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”]}“
Output:
Alice’s age is: 22
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.
Create a text file named names.txt:
Alice
Bob
Create a script to read each line:
while IFS= read -r line; do
echo “Name: $line“
done < names.txt
Output:
Name: Alice
Name: Bob
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.
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”
Output:
Cities have been saved to cities.txt
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.
sleep 5 &
echo “This message appears immediately, while the background job waits.”
wait
echo “Background job has completed.”
Output:
Background job has completed.
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.
echo “Altschool Africa is great!” > school.txt
sed -i ‘s/great/awesome/’ school.txt
cat school.txt
Output:
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.
Create a Data File students.csv:
Alice,Frontend Development
Bob,Data Science
Write the script:
awk -F, ‘{print $1}’ students.csv
Output:
Alice
Bob
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.
trap ‘echo “Exiting…”; exit’ SIGINT SIGTERM
echo “Running script. Press CTRL+C to stop.”
while :; do
sleep 1
done
Output (after pressing CTRL+C):
Exiting…
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!