A progressive set of exercises to help you master shell scripting step by step.
Focus: commands, variables, simple scripts
-
Write a script that prints:
- “Hello, World!”
- Your username
- Current date
- Ask the user for their name and age
- Print: “You will be X years old in 5 years”
-
Ask for a filename
-
Check:
- If it exists
- If it’s readable
- If it’s writable
-
Take two numbers as arguments
-
Output:
- Sum
- Difference
- Product
- Print numbers from 1 to 20
- Then print only even numbers
Focus: conditionals, loops, functions
- Copy all
.txtfiles from one folder to another - Add a timestamp to the backup folder name
-
Given a log file:
- Count number of lines
- Count occurrences of the word “ERROR”
Create a menu:
1. Show current directory
2. List files
3. Show disk usage
4. Exit
- Keep looping until user exits
-
Write functions:
is_evenfactorial
-
Call them from the script
-
Move files into folders based on extension:
.jpg→ images/.txt→ text/.sh→ scripts/
Focus: pipes, awk/sed, process handling
- Show top 5 processes by CPU usage
- Refresh every 5 seconds
- Show directories larger than a given size
- Sort them by size
-
Rename files:
- Replace spaces with underscores
- Add prefix or suffix
-
Given a CSV file:
- Print a specific column
- Filter rows based on a condition
- Pull latest code from a Git repo
- Restart a service
- Log the deployment
Focus: robustness, system tools, automation
-
Support flags like:
-f <file>-v(verbose)
-
Use
getopts
- Run multiple commands in parallel
- Wait for all to finish
- Collect exit statuses
-
Report:
- CPU usage
- Memory usage
- Disk usage
-
Output a nicely formatted report
-
Script to:
- Add cron jobs
- List cron jobs
- Remove cron jobs
-
Build a full CLI utility with:
- Help menu (
--help) - Subcommands
- Error handling
- Help menu (
-
Search for a pattern in a file
-
Support flags like:
- Case insensitive
- Line numbers
-
Implement a basic shell:
- Read command input
- Execute commands
- Handle
cd,exit
- Archive logs when they exceed a size
- Compress old logs
- Keep only the last N logs
- Use
curlto fetch data from an API - Parse JSON (using
jq) - Display formatted output
- Incremental backups
- Compression
- Remote sync (e.g.,
rsync)
-
Try solving exercises without looking up solutions first
-
Practice using:
grep,awk,sedcron,systemctl,ps
-
Add error handling:
set -etrap
Happy scripting! 🚀