Chapter 62: Bash Exercises
Bash Exercises
This is not just “theory time” anymore — this is the real muscle-building part of learning Bash. Everything we have covered so far (commands, variables, quoting, if-else, loops, functions, arrays, cron, permissions, compression, ssh/scp/rsync, curl, etc.) is like reading a book about swimming.
Exercises are jumping into the pool and actually swimming — making mistakes, fixing them, feeling frustrated for 5 minutes, then suddenly succeeding and smiling like “I did it!”
So today I will explain:
- What Bash exercises really are
- Why they are 10× more important than just reading
- How to do them properly (so you don’t waste time)
- A full set of graded exercises (easy → medium → hard)
- A small quiz at the end
- My personal tips from teaching hundreds of students in Hyderabad
Why Exercises Are the Secret to Becoming Good at Bash
- Bash is 90% muscle memory — typing [[ “$var” -ge 18 ]] correctly 50 times is worth more than reading about it 500 times.
- Most errors happen because of tiny syntax mistakes (missing quotes, wrong brackets, space around =) — you only fix them by doing, not reading.
- Real jobs / freelance / server work give you problems like: “Write a script that backs up only files modified in last 7 days” → You solve this by doing exercises, not by memorizing theory.
- When you finish an exercise and it works → you get real confidence → next script feels less scary.
Rule from teacher: For every 1 hour of reading/watching → do at least 2–3 hours of typing & debugging.
How to Do Bash Exercises the Right Way (My Method)
- Create a clean playground folder:
Bash0123456mkdir -p ~/bash_practice && cd ~/bash_practice
- For each exercise make a new file:
Bash0123456nano exercise_01.sh
- Write the code → save → make executable:
Bash0123456chmod +x exercise_01.sh
- Run it:
Bash0123456./exercise_01.sh
- If it fails → read the error message carefully (first line usually tells you where) → fix → try again → Stuck for >10 min? Look at hint → still stuck? Look at solution → then rewrite it yourself without looking.
- After it works → improve it: add comments, colors, better variable names, error handling.
- Keep all your exercises in one folder — in 2–3 months you will have a beautiful personal Bash library.
Graded Bash Exercises (Do Them in Order)
Level 1 – Very Easy (Basic Commands + Variables + Quoting)
Exercise 1 – System Snapshot Write system_info.sh that prints:
- Current date & time in format: “Wednesday 26-Feb-2025 14:30 IST”
- Your username
- Your current directory
- Your home directory
- Number of files (including hidden) in current directory
- Free space on root filesystem in human-readable format
Exercise 2 – Personalized Greeting Write greet_me.sh that:
- Asks for your name (read)
- Asks for your age
- Prints: “Namaste [name] ji! You are [age] years young! 😄” “Keep rocking in Hyderabad!”
Exercise 3 – Fix the Broken Script Copy this broken code into fix_me.sh, make executable, run it, find & fix at least 6 mistakes:
|
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
#!/bin/bash name = Rahul age=22 echo Hello $name you are $age years old city = "Hyderabad" echo I live in $city echo My home is $HOME |
Level 2 – Medium (if/else + loops + basic functions)
Exercise 4 – Smart Backup Checker Write smart_backup.sh that:
- Checks if folder ~/Documents exists
- If yes → creates backup name Documents_YYYY-MM-DD.tar.gz in ~/backups/
- If no → prints error and exits with status 1
- Bonus: use a function called create_backup
Exercise 5 – File Type Counter Write count_files.sh that:
- Loops over all files in current directory
- Counts how many files end with .txt, .jpg/.png, .pdf, .sh, others
- Prints summary like:
text01234567891011Text files (.txt): 12Images (.jpg/.png): 8PDFs: 5Scripts (.sh): 3Other files: 20Total files: 48
Exercise 6 – Grade & Motivation Write student_grade.sh that:
- Asks for marks (0–100)
- Uses if-elif-else to decide grade ≥90 → A+ “Genius level! Keep shining ✨” ≥80 → A “Very good — proud of you!” ≥70 → B “Solid effort — you can do better!” ≥60 → C “Average — time to study more” <60 → F “Don’t worry — failure is the first step to success 💪 Try again!”
Level 3 – Harder (Arrays + Functions + Loops + Conditions)
Exercise 7 – Folder Organizer Write organize_downloads.sh that:
- Looks at current folder (or pass folder as $1)
- Moves files to subfolders based on extension: .jpg/.png → Images/ .pdf → Documents/ .mp3/.mp4 → Media/ .zip/.tar.gz → Archives/ others → Others/
- Creates folders if they don’t exist
- Prints “Moved photo.jpg → Images/” for each file
Exercise 8 – Function Library Create tools.sh with these functions:
- print_success “message” → green text
- print_error “message” → red text
- backup_file source dest → copies file with date stamp
- check_space threshold → warns if / usage > threshold %
Then write use_tools.sh that calls them with different messages and checks.
Exercise 9 – Mini Cron Helper Write schedule_reminder.sh that:
- Asks for task name
- Asks for time (example: “every day at 8:00 AM”)
- Prints the correct crontab line Bonus: also prints “Run crontab -e and paste this:”
Mini Quiz (Answer Without Looking Back)
- What is wrong here? name = “Webliance”
- How do you safely loop over array elements that may contain spaces? a) for i in $array b) for i in ${array[@]} c) for i in “${array[@]}”
- Which is the correct way to check if a number is greater than 10? a) [ $num > 10 ] b) [[ $num > 10 ]] c) (( $num > 10 ))
- How do you make a function local variable? a) var=”value” b) local var=”value” c) VAR=”value”
- What does */15 * * * * in cron mean?
(Answers at the very bottom — no cheating!)
Final Teacher Advice
- Do at least 5 exercises per week — even if small
- Keep a folder ~/bash_practice/exercises/ — in 3 months you will have 60+ scripts
- When stuck → read error message first → google exact error → ask me last
- Celebrate every working script — even if it’s just “Hello world with colors”
You are doing great — now stop reading and start typing!
Which exercise do you want to start with? Or want hints/solutions for any? Or want harder ones (cron + functions + arrays together)?
Tell teacher — I’m ready in Hyderabad! Go practice — make mistakes — fix them — become unstoppable! 🐧💪📝 😄
Quiz Answers
- Space around = — should be name=”Webliance”
- c) for i in “${array[@]}”
- c) (( $num > 10 )) (or [[ $num -gt 10 ]])
- b) local var=”value”
- Every 15 minutes (0,15,30,45 past every hour)
Now go — create that folder and start! 🚀
