Linux Shell Scripting Tutorial: Bash Basics with Examples
Bash shell scripting allows you to automate repetitive tasks, execute multiple commands, and build powerful command-line utilities.
Shell scripts are widely used in Linux administration, DevOps, software deployment, and system automation.
This guide introduces Bash scripting fundamentals including variables, operators, conditions, loops, functions, and practical examples.
What is Bash?
Bash (Bourne Again SHell) is the default command-line shell on most Linux distributions. It interprets commands entered by users and executes shell scripts.
A Bash script is simply a text file containing Linux commands that execute sequentially.
Creating Your First Script
#!/bin/bash
echo "Hello, World!"
Save the file as hello.sh, make it executable using chmod +x hello.sh, and run it with ./hello.sh.
Script Execution
chmod +x hello.sh
./hello.sh
bash hello.sh
Variables
Variables store values that can be reused throughout a script.
#!/bin/bash
name="Alice"
age=25
echo "Name: $name"
echo "Age: $age"
Variables are assigned without spaces around the '=' symbol and accessed using the '$' prefix.
Reading User Input
#!/bin/bash
read -p "Enter your name: " username
echo "Welcome $username"
The read command accepts input from the keyboard and stores it in a variable.
Conditional Statements
Conditional statements allow scripts to make decisions based on specific conditions.
#!/bin/bash
age=20
if [ $age -ge 18 ]; then
echo "Adult"
else
echo "Minor"
fi
The if statement executes different code blocks depending on whether the condition evaluates to true or false.
Comparison Operators
-eq Equal to
-ne Not equal to
-gt Greater than
-lt Less than
-ge Greater than or equal
-le Less than or equal
Loops
Loops execute a block of code repeatedly until a condition is met.
For Loop
for i in 1 2 3 4 5
do
echo "Number: $i"
done
While Loop
count=1
while [ $count -le 5 ]
do
echo $count
((count++))
done
Loops are commonly used for automation, file processing, and repetitive administrative tasks.
Functions
Functions help organize reusable code into logical blocks.
greet() {
echo "Welcome $1"
}
greet Alice
Functions improve readability, reduce duplication, and simplify maintenance.
Command-Line Arguments
echo "Script Name: $0"
echo "First Argument: $1"
echo "Second Argument: $2"
echo "Total Arguments: $#"
Special variables allow scripts to access command-line arguments and execution details.
Useful Bash Operators
Arithmetic operators perform mathematical calculations.
Logical operators combine multiple conditions.
String operators compare text values.
num=$((5 + 10))
echo $num
Practical Example
#!/bin/bash
read -p "Enter a filename: " file
if [ -f "$file" ]; then
echo "File exists."
else
echo "File not found."
fi
This script checks whether a file exists and displays an appropriate message.
Applications
Automating backups and scheduled tasks.
Managing Linux servers and cloud infrastructure.
Automating software installation and deployment.
Monitoring system resources and generating reports.
Building DevOps automation pipelines.
Advantages
Simple syntax that is easy to learn.
Powerful automation capabilities.
Built into nearly every Linux distribution.
Works seamlessly with Linux commands and utilities.
Limitations
Not suitable for large, complex software projects.
Debugging long shell scripts can become challenging.
Performance is slower than compiled programming languages.
Best Practices
Always begin scripts with the correct shebang (#!/bin/bash).
Use meaningful variable and function names.
Comment complex logic to improve readability.
Validate user input before processing it.
Test scripts thoroughly before using them in production.
Mastering Bash shell scripting will help you automate Linux tasks, improve productivity, and build a strong foundation for DevOps and system administration.