Complete Bash Scripting Tutorial for Beginners to Advanced
Complete Bash Scripting Tutorial for Beginners to Advanced
Bash scripting is one of the most powerful skills for Linux users, DevOps engineers, backend developers, cybersecurity professionals, and system administrators. In this tutorial, you will learn Bash shell scripting from beginner to advanced with practical examples and automation scripts.
What is Bash Scripting?
Bash scripting is the process of writing commands in a file to automate Linux and Unix tasks. Bash stands for Bourne Again Shell and is the default shell for most Linux distributions like Ubuntu, Debian, Kali Linux, and CentOS.
Benefits of Bash Scripting
- Automate repetitive tasks
- Manage Linux servers
- Create deployment scripts
- Perform backups automatically
- Monitor system performance
- Useful for DevOps and Cybersecurity
- Easy command-line automation
Bash Script Structure
A basic Bash script contains:
#!/bin/bash
echo "Hello World"
Explanation
| Part | Meaning |
|---|---|
#!/bin/bash | Shebang line |
echo | Prints text |
.sh | Bash script extension |
Create Your First Bash Script
Step 1: Create File
nano hello.sh
Step 2: Add Code
#!/bin/bash
echo "Welcome to Tech3Space"
Step 3: Give Permission
chmod +x hello.sh
Step 4: Run Script
./hello.sh
Output:
Welcome to Tech3Space
Bash Variables
Variables store values in Bash scripting.
Example
#!/bin/bash
name="Ankit"
echo "Hello $name"
Output:
Hello Ankit
User Input in Bash
Use read command to take user input.
#!/bin/bash
echo "Enter your name:"
read username
echo "Welcome $username"
Bash Comments
Comments help explain code.
Single Line Comment
# This is a comment
Conditional Statements
Conditional statements control decision making.
If Statement
#!/bin/bash
number=10
if [ $number -gt 5 ]
then
echo "Number is greater than 5"
fi
If Else Example
#!/bin/bash
age=18
if [ $age -ge 18 ]
then
echo "Eligible to vote"
else
echo "Not eligible"
fi
Bash Loops
Loops repeat tasks automatically.
For Loop
#!/bin/bash
for i in 1 2 3 4 5
do
echo $i
done
While Loop
#!/bin/bash
count=1
while [ $count -le 5 ]
do
echo $count
((count++))
done
Functions in Bash
Functions organize reusable code.
#!/bin/bash
greet() {
echo "Welcome to Tech3Space"
}
greet
Bash Script Arguments
Arguments pass values to scripts.
#!/bin/bash
echo "First Argument: $1"
echo "Second Argument: $2"
Run:
./script.sh Linux Ubuntu
File Handling in Bash
Create File
touch demo.txt
Delete File
rm demo.txt
Check File Exists
if [ -f demo.txt ]
then
echo "File exists"
fi
Directory Operations
Create Directory
mkdir project
Remove Directory
rmdir project
Useful Bash Operators
| Operator | Meaning |
|---|---|
-eq | Equal |
-ne | Not Equal |
-gt | Greater Than |
-lt | Less Than |
-ge | Greater Than Equal |
-le | Less Than Equal |
Bash Automation Script Example
Automatic Backup Script
#!/bin/bash
backup_dir="/backup"
source_dir="/home/user/data"
tar -czf $backup_dir/backup.tar.gz $source_dir
echo "Backup Completed"
System Monitoring Script
#!/bin/bash
echo "CPU Usage:"
top -bn1 | grep Cpu
echo "Memory Usage:"
free -m
Bash Script for DevOps
Bash scripting is heavily used in:
- Docker automation
- Kubernetes deployment
- CI/CD pipelines
- Server monitoring
- Linux administration
- Cloud automation
Common Bash Commands
| Command | Description |
|---|---|
ls | List files |
pwd | Current directory |
cd | Change directory |
cp | Copy files |
mv | Move files |
grep | Search text |
find | Find files |
chmod | Change permissions |
ps | Show processes |
kill | Stop process |
Bash Script Best Practices
- Use comments properly
- Keep scripts modular
- Validate user input
- Use meaningful variable names
- Test scripts before production
- Handle errors carefully
Advantages of Bash Scripting
- Fast automation
- Lightweight
- Easy to learn
- Built into Linux systems
- Great for server management
Bash Scripting Interview Questions
What is Bash?
Bash is a command-line shell and scripting language used in Linux systems.
What is Shebang?
The shebang line tells the system which interpreter to use.
Example:
#!/bin/bash
How to make script executable?
chmod +x script.sh
Conclusion
Bash scripting is an essential Linux skill for developers, DevOps engineers, ethical hackers, and system administrators. Learning shell scripting helps automate tasks, manage servers, and improve productivity in Linux environments.
Start practicing small scripts daily and gradually move toward advanced automation projects.
Related Tutorials
- Linux Commands Tutorial
- Ubuntu Server Guide
- Docker Beginner Tutorial
- Kubernetes Notes
- DevOps Roadmap
- Shell Scripting Projects
Frequently Asked Questions
Is Bash scripting easy to learn?
Yes, Bash scripting is beginner-friendly and perfect for Linux automation.
Which operating systems support Bash?
Linux, macOS, Ubuntu, Debian, Kali Linux, CentOS, and WSL on Windows.
Is Bash useful for DevOps?
Yes, Bash is widely used in DevOps automation and server management.
Can I use Bash for cybersecurity?
Yes, ethical hackers and penetration testers use Bash scripting for automation and security testing.