π§ Understanding Linux Shells & Package Managers
π Popular Linux Shells
π¦ Linux Package Managers
π What is Bash?
π§ What is Bash Scripting?
β‘ Why Bash Automation Matters
π Bash Script Structure
π₯ Variables in Bash
β
Creating Variables
β οΈ Important Rule
π§Ύ User Input
π Command Line Arguments
π Conditional Statements
β
If Statement
β
If-Else
β
If-Elif-Else
π§ Comparison Operators
π Loops in Bash
β
For Loop
β
Range Loop
β
While Loop
β
Infinite Loop
π¨ Functions in Bash
π¦ Arrays in Bash
π File Operations
β
Check if File Exists
β
Create File
β
Append to File
βοΈ Process Automation
β
Kill Process
β
Restart Service
β
Check Service Status
π Cron Jobs for Scheduling
β
Run Every Day at Midnight
β
Run Every 5 Minutes
π Real-World Automation Scripts
π₯ 1οΈβ£ Automated Backup Script
π₯ 2οΈβ£ Disk Usage Monitoring Script
π₯ 3οΈβ£ Website Monitoring Script
π₯ 4οΈβ£ Auto Deployment Script
π₯ 5οΈβ£ Log Cleanup Script
π‘οΈ Error Handling in Bash
π Exit Codes
π§ͺ Debugging Bash Scripts
β
Run in Debug Mode
β
Strict Mode (Highly Recommended)
β‘ Bash Automation in DevOps
π Security Best Practices
β Avoid Hardcoding Passwords
β
Quote Variables
β
Validate Inputs
π Important Bash Commands Every Engineer Should Know
π Bash vs Python for Automation
π― Best Practices for Bash Scripting
π§ Final Thoughts Modern infrastructure runs on automation. You will eventually write Bash scripts. From automating backups to deploying servers, monitoring systems, managing logs, and running CI/CD pipelines β Bash is everywhere. If Linux is the operating system of the internet, then Bash is its automation language. GitHub Repo:
https://github.com/17J/30-Days-Cloud-DevSecOps-Journey Command Sheet:https://bash-command-sheets-k51c.vercel.app/ Linux Cron Job Scheduler:https://crontab.guru/ many beginners get confused between: So before learning Bash scripting, letβs first understand the Linux shell ecosystem. Linux has multiple shells. A shell is simply a command interpreter that lets users communicate with the operating system. Package managers install software. Different Linux distributions use different package managers. It is the default shell for most Linux distributions. A shell is simply a program that allows users to interact with the operating system. These commands are executed through the shell. A Bash script is a file containing Linux commands executed sequentially. Instead of manually typing commands repeatedly, you automate them inside a script. β Manual server setupβ Repetitive deploymentsβ Manual backupsβ Human errorsβ Slow operations With Bash automation: β
Faster workflowsβ
Repeatable processesβ
Infrastructure consistencyβ
Reduced human mistakesβ
Better productivity Variables store data. Access variables using $. Take input dynamically. Arguments passed while running scripts. Conditions allow decision-making. Loops repeat tasks automatically. Functions help organize reusable code. Cron automates scripts at scheduled times. β
Compresses filesβ
Creates timestamp backupβ
Automates backup process Deletes logs older than 7 days. Always validate failures. $? stores previous command status. Shows command execution step-by-step. Bash is heavily used in: Never write insecure scripts. Always sanitize user input. Most DevOps engineers use both. β
Use meaningful variable namesβ
Add commentsβ
Use functionsβ
Handle errors properlyβ
Use strict modeβ
Keep scripts modularβ
Log important actions
β
Test before production Bash scripting is one of the most valuable skills in Linux, DevOps, Cloud, and Cybersecurity. The engineers who automate repetitive tasks become exponentially more productive. Over time, Bash becomes your operational superpower. Templates let you quickly answer FAQs or store snippets for re-use. Hide child comments as well For further actions, you may consider blocking this person and/or reporting abuse
User β Shell β Linux Kernel β Hardware
User β Shell β Linux Kernel β Hardware
User β Shell β Linux Kernel β Hardware
pwd
ls
mkdir project
pwd
ls
mkdir project
pwd
ls
mkdir project
#!/bin/bash echo "Hello Rahul"
date
uptime
#!/bin/bash echo "Hello Rahul"
date
uptime
#!/bin/bash echo "Hello Rahul"
date
uptime
chmod +x hello.sh
chmod +x hello.sh
chmod +x hello.sh
#!/bin/bash # Comments start with # echo "Starting Script" # Commands here
#!/bin/bash # Comments start with # echo "Starting Script" # Commands here
#!/bin/bash # Comments start with # echo "Starting Script" # Commands here
name="Rahul"
age=22
city="Delhi"
name="Rahul"
age=22
city="Delhi"
name="Rahul"
age=22
city="Delhi"
echo $name
echo $age
echo $name
echo $age
echo $name
echo $age
name = "Rahul"
name = "Rahul"
name = "Rahul"
name="Rahul"
name="Rahul"
name="Rahul"
#!/bin/bash echo "Enter your name:"
read name echo "Welcome $name"
#!/bin/bash echo "Enter your name:"
read name echo "Welcome $name"
#!/bin/bash echo "Enter your name:"
read name echo "Welcome $name"
#!/bin/bash echo "First argument: $1"
echo "Second argument: $2"
#!/bin/bash echo "First argument: $1"
echo "Second argument: $2"
#!/bin/bash echo "First argument: $1"
echo "Second argument: $2"
./script.sh Rahul Linux
./script.sh Rahul Linux
./script.sh Rahul Linux
First argument: Rahul
Second argument: Linux
First argument: Rahul
Second argument: Linux
First argument: Rahul
Second argument: Linux
#!/bin/bash age=20 if [ $age -ge 18 ]
then echo "Adult"
fi
#!/bin/bash age=20 if [ $age -ge 18 ]
then echo "Adult"
fi
#!/bin/bash age=20 if [ $age -ge 18 ]
then echo "Adult"
fi
#!/bin/bash num=5 if [ $num -gt 10 ]
then echo "Greater than 10"
else echo "Less than or equal to 10"
fi
#!/bin/bash num=5 if [ $num -gt 10 ]
then echo "Greater than 10"
else echo "Less than or equal to 10"
fi
#!/bin/bash num=5 if [ $num -gt 10 ]
then echo "Greater than 10"
else echo "Less than or equal to 10"
fi
#!/bin/bash marks=75 if [ $marks -ge 90 ]
then echo "Grade A"
elif [ $marks -ge 70 ]
then echo "Grade B"
else echo "Grade C"
fi
#!/bin/bash marks=75 if [ $marks -ge 90 ]
then echo "Grade A"
elif [ $marks -ge 70 ]
then echo "Grade B"
else echo "Grade C"
fi
#!/bin/bash marks=75 if [ $marks -ge 90 ]
then echo "Grade A"
elif [ $marks -ge 70 ]
then echo "Grade B"
else echo "Grade C"
fi
#!/bin/bash for i in 1 2 3 4 5
do echo "Number: $i"
done
#!/bin/bash for i in 1 2 3 4 5
do echo "Number: $i"
done
#!/bin/bash for i in 1 2 3 4 5
do echo "Number: $i"
done
for i in {1..10}
do echo $i
done
for i in {1..10}
do echo $i
done
for i in {1..10}
do echo $i
done
#!/bin/bash count=1 while [ $count -le 5 ]
do echo $count ((count++))
done
#!/bin/bash count=1 while [ $count -le 5 ]
do echo $count ((count++))
done
#!/bin/bash count=1 while [ $count -le 5 ]
do echo $count ((count++))
done
while true
do echo "Running..." sleep 2
done
while true
do echo "Running..." sleep 2
done
while true
do echo "Running..." sleep 2
done
#!/bin/bash greet() { echo "Hello $1"
} greet Rahul
#!/bin/bash greet() { echo "Hello $1"
} greet Rahul
#!/bin/bash greet() { echo "Hello $1"
} greet Rahul
#!/bin/bash fruits=("apple" "banana" "mango") echo ${fruits[0]}
echo ${fruits[1]}
#!/bin/bash fruits=("apple" "banana" "mango") echo ${fruits[0]}
echo ${fruits[1]}
#!/bin/bash fruits=("apple" "banana" "mango") echo ${fruits[0]}
echo ${fruits[1]}
for fruit in "${fruits[@]}"
do echo $fruit
done
for fruit in "${fruits[@]}"
do echo $fruit
done
for fruit in "${fruits[@]}"
do echo $fruit
done
#!/bin/bash if [ -f test.txt ]
then echo "File exists"
else echo "File not found"
fi
#!/bin/bash if [ -f test.txt ]
then echo "File exists"
else echo "File not found"
fi
#!/bin/bash if [ -f test.txt ]
then echo "File exists"
else echo "File not found"
fi
touch file.txt
touch file.txt
touch file.txt
echo "New Log Entry" >> logs.txt
echo "New Log Entry" >> logs.txt
echo "New Log Entry" >> logs.txt
pkill nginx
pkill nginx
pkill nginx
systemctl restart nginx
systemctl restart nginx
systemctl restart nginx
systemctl status docker
systemctl status docker
systemctl status docker
0 0 * * * /home/ubuntu/backup.sh
0 0 * * * /home/ubuntu/backup.sh
0 0 * * * /home/ubuntu/backup.sh
*/5 * * * * /home/ubuntu/monitor.sh
*/5 * * * * /home/ubuntu/monitor.sh
*/5 * * * * /home/ubuntu/monitor.sh
#!/bin/bash SOURCE="/home/ubuntu/data"
DEST="/backup" DATE=$(date +%Y-%m-%d) tar -czf $DEST/backup-$DATE.tar.gz $SOURCE echo "Backup completed"
#!/bin/bash SOURCE="/home/ubuntu/data"
DEST="/backup" DATE=$(date +%Y-%m-%d) tar -czf $DEST/backup-$DATE.tar.gz $SOURCE echo "Backup completed"
#!/bin/bash SOURCE="/home/ubuntu/data"
DEST="/backup" DATE=$(date +%Y-%m-%d) tar -czf $DEST/backup-$DATE.tar.gz $SOURCE echo "Backup completed"
#!/bin/bash THRESHOLD=80 USAGE=$(df / | grep / | awk '{ print $5 }' | sed 's/%//g') if [ $USAGE -gt $THRESHOLD ]
then echo "Disk usage exceeded threshold!"
else echo "Disk usage normal"
fi
#!/bin/bash THRESHOLD=80 USAGE=$(df / | grep / | awk '{ print $5 }' | sed 's/%//g') if [ $USAGE -gt $THRESHOLD ]
then echo "Disk usage exceeded threshold!"
else echo "Disk usage normal"
fi
#!/bin/bash THRESHOLD=80 USAGE=$(df / | grep / | awk '{ print $5 }' | sed 's/%//g') if [ $USAGE -gt $THRESHOLD ]
then echo "Disk usage exceeded threshold!"
else echo "Disk usage normal"
fi
#!/bin/bash URL="https://example.com" STATUS=$(curl -o /dev/null -s -w "%{http_code}" $URL) if [ $STATUS -eq 200 ]
then echo "Website is UP"
else echo "Website is DOWN"
fi
#!/bin/bash URL="https://example.com" STATUS=$(curl -o /dev/null -s -w "%{http_code}" $URL) if [ $STATUS -eq 200 ]
then echo "Website is UP"
else echo "Website is DOWN"
fi
#!/bin/bash URL="https://example.com" STATUS=$(curl -o /dev/null -s -w "%{http_code}" $URL) if [ $STATUS -eq 200 ]
then echo "Website is UP"
else echo "Website is DOWN"
fi
#!/bin/bash echo "Pulling latest code..." git pull origin main echo "Installing dependencies..." npm install echo "Restarting application..." pm2 restart app
#!/bin/bash echo "Pulling latest code..." git pull origin main echo "Installing dependencies..." npm install echo "Restarting application..." pm2 restart app
#!/bin/bash echo "Pulling latest code..." git pull origin main echo "Installing dependencies..." npm install echo "Restarting application..." pm2 restart app
#!/bin/bash find /var/log -type f -name "*.log" -mtime +7 -delete echo "Old logs deleted"
#!/bin/bash find /var/log -type f -name "*.log" -mtime +7 -delete echo "Old logs deleted"
#!/bin/bash find /var/log -type f -name "*.log" -mtime +7 -delete echo "Old logs deleted"
#!/bin/bash mkdir test if [ $? -eq 0 ]
then echo "Directory created"
else echo "Failed"
fi
#!/bin/bash mkdir test if [ $? -eq 0 ]
then echo "Directory created"
else echo "Failed"
fi
#!/bin/bash mkdir test if [ $? -eq 0 ]
then echo "Directory created"
else echo "Failed"
fi
bash -x script.sh
bash -x script.sh
bash -x script.sh
#!/bin/bash set -euo pipefail
#!/bin/bash set -euo pipefail
#!/bin/bash set -euo pipefail
password="admin123"
password="admin123"
password="admin123"
read -s password
read -s password
read -s password
rm -rf $dir
rm -rf $dir
rm -rf $dir
rm -rf "$dir"
rm -rf "$dir"
rm -rf "$dir" - DevOps Engineer
- Linux Administrator
- Cloud Engineer
- Cybersecurity Professional
- Backend Developer - GitHub Repo:
https://github.com/17J/30-Days-Cloud-DevSecOps-Journey
- Command Sheet:
https://bash-command-sheets-k51c.vercel.app/
- Linux Cron Job Scheduler:
https://crontab.guru/ - Production systems - Production deployments - 0 = success
- Non-zero = failure - Undefined variables
- Failed commands
- Pipeline failures - Automate backups
- Monitor servers
- Deploy applications
- Schedule tasks