\newpage
BASH (Bourne Again SHell) scripting is a powerful tool in Unix-based systems that allows users to automate repetitive tasks, streamline workflows, and manage system processes efficiently. This chapter introduces you to the fundamentals of BASH scripting, including syntax, common constructs, and practical examples.
A BASH script is a text file containing a series of commands that the BASH shell executes sequentially. Scripts are used for automation, system management, and even building simple applications.
- Automation: Eliminate repetitive manual tasks.
- Efficiency: Simplify complex command sequences.
- Portability: Write scripts that run on any Unix-based system.
- Customization: Tailor scripts to specific needs.
- Open a terminal and create a new file:
touch first_script.sh
- Open the file in a text editor (e.g., nano, vim, or VS Code).
nano first_script.sh
- Add the following content:
#!/bin/bash echo "Hello, World!"
#!/bin/bash
specifies the interpreter for the script.echo
outputs text to the terminal.
- Change the file permissions:
chmod +x first_script.sh
- Run the script:
./first_script.sh
You should see:
Hello, World!
Variables store data for use within a script.
name="John"
echo "Hello, $name!"
Output:
Hello, John!
Use the read
command to accept input:
#!/bin/bash
read -p "Enter your name: " user_name
echo "Welcome, $user_name!"
- Write a script that asks for a user’s favorite programming language and displays a personalized message.
Perform actions based on conditions.
#!/bin/bash
if [ "$1" == "hello" ]; then
echo "Hi there!"
else
echo "Try saying 'hello'!"
fi
$1
represents the first argument passed to the script.
Automate repetitive tasks using loops.
for i in {1..5}; do
echo "Iteration $i"
done
count=1
while [ $count -le 5 ]; do
echo "Count: $count"
((count++))
done
Define reusable blocks of code within a script:
#!/bin/bash
greet() {
echo "Hello, $1!"
}
greet "John"
Output:
Hello, John!
Run a script with bash -x
to see each command as it executes:
bash -x script.sh
Use set
to enable debugging within a script:
set -x
# commands
set +x
- Use Comments: Document your scripts for clarity.
# This script greets the user
- Error Handling: Check for errors using
if
statements and exit codes. - Modularize: Break large scripts into functions.
- Test Iteratively: Test small sections of your script to identify issues early.
Create a script that:
- Accepts a directory path from the user.
- Checks if the directory exists.
- Lists the contents if it exists or displays an error if it doesn’t.
#!/bin/bash
read -p "Enter directory path: " dir
if [ -d "$dir" ]; then
echo "Contents of $dir:"
ls "$dir"
else
echo "Directory not found."
fi
In this chapter, you learned:
- How to write and execute BASH scripts.
- Using variables, user input, and control structures.
- Defining and using functions.
- Debugging and best practices.
With BASH scripting, you can automate tasks and build a strong foundation for more complex system management. In the next chapter, we’ll explore version control with Git, an essential tool for collaborative development.