🚀 Shell Scripting for DevOps: Zero-to-Hero Guide- Part 1
*A comprehensive guide to mastering shell scripting fundamentals for your DevOps journey*
— -
## 📌 Introduction
Welcome to the world of shell scripting! As I embark on my DevOps learning journey, I’ve discovered that shell scripting is not just a skill — it’s the backbone of automation and efficiency in the DevOps ecosystem. This guide captures everything I’ve learned so far, from basic Linux commands to writing powerful automation scripts.
— -
## 💡 1. The Power of Automation and Shell Scripting
### Why Automation Matters
In the fast-paced world of DevOps, **automation is not optional — it’s essential**. Imagine having to manually create hundreds of configuration files, or checking the health status of dozens of servers one by one. That’s where shell scripting comes to the rescue.
### What is Shell Scripting?
Shell scripting allows you to write sequences of commands that your Linux system executes automatically. Think of it as teaching your computer to perform repetitive tasks while you focus on more strategic work.
**Real-world scenarios where shell scripting shines:**
- 🔄 Automating deployments across multiple servers
- 📊 Collecting system metrics from a fleet of virtual machines
- 🛠️ Setting up development environments
- 📁 Managing files and directories at scale
- 🔍 Monitoring and alerting systems
— -
## 🔧 2. Essential Linux Commands for DevOps Engineers
Mastering these commands is your first step toward becoming a proficient DevOps engineer. Let’s explore each one with practical examples.
### 📝 `touch` — Create Files
The `touch` command creates new, empty files instantly.
```bash
# Create a single file
$ touch mynewscript.sh
# Create multiple files at once
$ touch config1.txt config2.txt config3.txt
```
**DevOps Use Case:** Quickly generate template files for configurations or scripts.
— -
### 📂 `ls` — List Directory Contents
The `ls` command displays files and directories. With options, it becomes incredibly powerful.
```bash
# Basic listing
$ ls
# Detailed listing with timestamps (sorted by time, reverse order)
$ ls -ltr
# Show hidden files too
$ ls -la
# Human-readable file sizes
$ ls -lh
# Output example:
-rw-r — r — 1 user group 2048 Mar 02 10:30 config.yaml
-rwxr-xr-x 1 user group 1024 Mar 02 09:15 deploy.sh
drwxr-xr-x 2 user group 4096 Mar 01 14:20 scripts/
```
— -
### 📖 `man` — The Manual Pages
Your built-in documentation system. Never forget a command again!
```bash
# Get help for any command
$ man ls
$ man chmod
$ man grep
# Quick search for a command
$ man -k “search term”
```
**Pro Tip:** Press `q` to exit the manual, and use arrow keys or space to navigate.
— -
### ✏️ `vi` / `vim` — Text Editors
Vim is the editor you’ll use everywhere — from editing configs on remote servers to writing scripts locally.
```bash
# Open or create a file
$ vim myscript.sh
```
**Essential Vim Commands:**
| Mode | Key | Action |
| — — — | — — -| — — — — |
| Normal | `i` | Enter Insert mode (start typing) |
| Normal | `Esc` | Return to Normal mode |
| Normal | `:w` | Save file |
| Normal | `:q` | Quit |
| Normal | `:wq` | Save and quit |
| Normal | `:q!` | Quit without saving |
| Normal | `dd` | Delete current line |
| Normal | `yy` | Copy current line |
| Normal | `p` | Paste |
**Quick Start Workflow:**
1. `vim script.sh` — Open file
2. Press `i` to start typing
3. Write your code
4. Press `Esc` when done
5. Type `:wq` and press Enter
— -
### 💬 `echo` — Display Text
Print messages or variable values to the terminal.
```bash
# Simple message
$ echo “Hello, DevOps World!”
# Display variable values
$ NAME=”Varshini”
$ echo “Welcome, $NAME!”
# Write to a file
$ echo “# My deployment script” > deploy.sh
```
— -
### 📜 `history` — Command History
View all previously executed commands.
```bash
# Show all history
$ history
# Show last 20 commands
$ history 20
# Re-run command number 42
$ !42
```
**Pro Tip:** Press `Ctrl+R` to search through your command history interactively.
— -
### 📍 `pwd` — Present Working Directory
Always know where you are in the filesystem.
```bash
$ pwd
/home/varshini/devops-projects
```
— -
### 📁 `mkdir` — Make Directory
Create new directories to organize your work.
```bash
# Create a single directory
$ mkdir myproject
# Create nested directories
$ mkdir -p projects/devops/scripts
# Create multiple directories
$ mkdir dir1 dir2 dir3
```
— -
### 🚶 `cd` — Change Directory
Navigate through your filesystem.
```bash
# Go to a specific directory
$ cd /var/log
# Go to home directory
$ cd ~
# or just
$ cd
# Go up one level
$ cd ..
# Go to previous directory
$ cd -
# Go to a subdirectory
$ cd projects/scripts
```
— -
### 🗑️ `rm` — Remove Files/Directories
Delete files and directories. **Use with caution!**
```bash
# Remove a file
$ rm oldscript.sh
# Remove multiple files
$ rm file1.txt file2.txt
# Remove a directory and its contents (DANGEROUS!)
$ rm -rf old_project/
# Remove with confirmation prompt (safer)
$ rm -i important_file.txt
```
⚠️ **Warning:** `rm -rf` is irreversible! Always double-check before using it.
— -
### 🔐 `chmod` — Change Permissions
Control who can read, write, or execute your files.
```bash
# Make a script executable
$ chmod +x myscript.sh
# Set specific permissions (rwx for owner, rx for group, rx for others)
$ chmod 755 deploy.sh
Get Varshini R’s stories in your inbox
Join Medium for free to get updates from this writer.
# Make fully accessible (use sparingly!)
$ chmod 777 test_script.sh
```
**Permission Numbers Explained:**
```
7 = read (4) + write (2) + execute (1) = rwx
6 = read (4) + write (2) = rw-
5 = read (4) + execute (1) = r-x
4 = read only = r —
Example: 755 means:
- Owner: rwx (7)
- Group: r-x (5)
- Others: r-x (5)
```
**After making executable:**
```bash
$ chmod +x myscript.sh
$ ./myscript.sh # Now you can run it!
```
— -
## 🎯 3. Understanding the Shebang: `#!/bin/bash`
### What is a Shebang?
The shebang (`#!`) is the **first line** of your script that tells the system which interpreter to use.
```bash
#!/bin/bash
```
### Why It Matters
Different shells have different syntax and features:
- **Bash** (`/bin/bash`): Full-featured, most common
- **Dash** (`/bin/dash`): Lightweight, faster but fewer features
- **Ksh** (`/bin/ksh`): Korn shell
- **Zsh** (`/bin/zsh`): Modern, feature-rich
### The `/bin/sh` Confusion
```bash
# On older systems
/bin/sh → /bin/bash
# On modern Ubuntu/Debian
/bin/sh → /bin/dash # Different shell!
```
### ✅ DevOps Best Practice
**Always use `#!/bin/bash` explicitly!**
This ensures your script runs with Bash, regardless of what `/bin/sh` points to.
**Guarantees consistency across:**
- Different Linux distributions
- Various server environments
- CI/CD pipelines
- Team members’ machines
— -
## 🛠️ 4. Creating Your First Shell Script
Let’s put everything together and create a functional automation script!
### Step-by-Step Guide
**Step 1: Create the Script File**
```bash
$ vim myfirstscript.sh
```
**Step 2–4: Write the Complete Script**
```bash
#!/bin/bash
# ========================================
# DevOps Automation Script
# Purpose: Set up project directory structure
# Author: Varshini
# Date: March 2, 2026
# ========================================
# Display start message
echo “🚀 Starting project setup…”
# Create main project directory
echo “📁 Creating project directory…”
mkdir mynewdevops_folder
# Navigate into the directory
cd mynewdevops_folder
# Create configuration files
echo “📝 Creating configuration files…”
touch server_config.txt
touch deploy_log.txt
touch environment_vars.sh
# Create subdirectories
echo “📂 Creating folder structure…”
mkdir -p scripts logs backups
# Display completion message
echo “✅ Project setup complete!”
echo “📍 Location: $(pwd)”
ls -la
```
**Step 5: Save the File**
In vim:
1. Press `Esc`
2. Type `:wq`
3. Press `Enter`
**Step 6: Make It Executable**
```bash
$ chmod +x myfirstscript.sh
```
**Step 7: Run Your Script!**
```bash
$ ./myfirstscript.sh
```
**Expected Output:**
```
🚀 Starting project setup…
📁 Creating project directory…
📝 Creating configuration files…
📂 Creating folder structure…
✅ Project setup complete!
📍 Location: /home/varshini/mynewdevops_folder
total 20
drwxr-xr-x 5 varshini varshini 4096 Mar 02 10:45 .
drwxr-xr-x 8 varshini varshini 4096 Mar 02 10:45 ..
drwxr-xr-x 2 varshini varshini 4096 Mar 02 10:45 backups
-rw-r — r — 1 varshini varshini 0 Mar 02 10:45 deploy_log.txt
-rw-r — r — 1 varshini varshini 0 Mar 02 10:45 environment_vars.sh
drwxr-xr-x 2 varshini varshini 4096 Mar 02 10:45 logs
drwxr-xr-x 2 varshini varshini 4096 Mar 02 10:45 scripts
-rw-r — r — 1 varshini varshini 0 Mar 02 10:45 server_config.txt
```
— -
## 🌟 5. Shell Scripting’s Role in DevOps
Shell scripting isn’t just a nice-to-have skill — it’s **fundamental** to modern DevOps practices.
### Infrastructure Automation
```bash
#!/bin/bash
# Automated server provisioning script
# Update system packages
sudo apt update && sudo apt upgrade -y
# Install required packages
sudo apt install -y nginx docker.io git
# Configure firewall
sudo ufw allow 80
sudo ufw allow 443
sudo ufw enable
echo “✅ Server provisioned successfully!”
```
### Configuration Management
```bash
#!/bin/bash
# Bootstrap configuration management
# Install Ansible
sudo apt install -y ansible
# Clone configuration repository
git clone https://github.com/company/ansible-configs.git
# Run initial playbook
cd ansible-configs
ansible-playbook -i inventory/production site.yml
```
### CI/CD Pipeline Integration
```bash
#!/bin/bash
# Deployment script for CI/CD
# Run tests
npm test
# Build application
npm run build
# Deploy to production
rsync -avz dist/ user@production-server:/var/www/app/
# Restart services
ssh user@production-server ‘sudo systemctl restart nginx’
echo “🚀 Deployment complete!”
```
### Monitoring and Health Checks
```bash
#!/bin/bash
# Automated server health monitoring
SERVERS=(“server1.example.com” “server2.example.com” “server3.example.com”)
ALERT_EMAIL=”ops-team@company.com”
for server in “${SERVERS[@]}”; do
# Check CPU usage
CPU=$(ssh $server “top -bn1 | grep ‘Cpu(s)’ | awk ‘{print \$2}’”)
# Check RAM usage
RAM=$(ssh $server “free | grep Mem | awk ‘{print (\$3/\$2) * 100.0}’”)
# Check disk usage
DISK=$(ssh $server “df -h / | tail -1 | awk ‘{print \$5}’ | sed ‘s/%//’”)
echo “Server: $server”
echo “ CPU: ${CPU}%”
echo “ RAM: ${RAM}%”
echo “ Disk: ${DISK}%”
# Alert if thresholds exceeded
if (( $(echo “$CPU > 80” | bc -l) )); then
echo “⚠️ High CPU on $server” | mail -s “Alert: High CPU” $ALERT_EMAIL
fi
done
```
### Backup Automation
```bash
#!/bin/bash
# Automated backup script
BACKUP_DIR=”/backups”
DATE=$(date +%Y%m%d_%H%M%S)
DB_NAME=”production_db”
# Create backup
mysqldump -u root -p$DB_PASSWORD $DB_NAME > “$BACKUP_DIR/db_$DATE.sql”
# Compress backup
gzip “$BACKUP_DIR/db_$DATE.sql”
# Upload to cloud storage
aws s3 cp “$BACKUP_DIR/db_$DATE.sql.gz” s3://company-backups/databases/
# Clean up old backups (keep last 7 days)
find $BACKUP_DIR -name “db_*.sql.gz” -mtime +7 -delete
echo “✅ Backup completed: db_$DATE.sql.gz”
```
— -
## 🎓 Key Takeaways
### What I’ve Learned So Far:
1. ✅ **Automation is essential** — Shell scripting saves hours of manual work
2. ✅ **Master the basics** — Core Linux commands are your foundation
3. ✅ **Always use `#!/bin/bash`** — Ensures consistency across environments
4. ✅ **Comments are crucial** — Future you (and your team) will thank you
5. ✅ **Permissions matter** — `chmod` makes scripts executable
6. ✅ **Start small** — Begin with simple scripts and build complexity gradually
## 🚀 Conclusion
Shell scripting is the bridge between knowing Linux commands and becoming a true DevOps engineer. Every automation script you write, no matter how small, is a step toward mastering this critical skill.
As I continue this journey, I’m excited to dive deeper into advanced topics and build more sophisticated automation tools. The foundation is set, and the possibilities are endless!
**Remember:** Every expert DevOps engineer started exactly where you are now — with their first `#!/bin/bash`.
— -
*Happy Scripting! 🎉*
*Follow my DevOps learning journey for more guides and insights.*
— -
**Tags:** #DevOps #ShellScripting #Linux #Automation #Bash #Learning #TechJourney



