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

  1. Bash is 90% muscle memory — typing [[ “$var” -ge 18 ]] correctly 50 times is worth more than reading about it 500 times.
  2. Most errors happen because of tiny syntax mistakes (missing quotes, wrong brackets, space around =) — you only fix them by doing, not reading.
  3. 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.
  4. 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)

  1. Create a clean playground folder:
    Bash
  2. For each exercise make a new file:
    Bash
  3. Write the code → save → make executable:
    Bash
  4. Run it:
    Bash
  5. 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.
  6. After it works → improve it: add comments, colors, better variable names, error handling.
  7. 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:

Bash

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:
    text

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)

  1. What is wrong here? name = “Webliance”
  2. 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[@]}”
  3. Which is the correct way to check if a number is greater than 10? a) [ $num > 10 ] b) [[ $num > 10 ]] c) (( $num > 10 ))
  4. How do you make a function local variable? a) var=”value” b) local var=”value” c) VAR=”value”
  5. 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

  1. Space around = — should be name=”Webliance”
  2. c) for i in “${array[@]}”
  3. c) (( $num > 10 )) (or [[ $num -gt 10 ]])
  4. b) local var=”value”
  5. Every 15 minutes (0,15,30,45 past every hour)

Now go — create that folder and start! 🚀

You may also like...

Leave a Reply

Your email address will not be published. Required fields are marked *