Skip to main content

Command Palette

Search for a command to run...

Shell Scripting Didn’t Make Sense to Me Until I Built These Two Scripts

Published
4 min read
Shell Scripting Didn’t Make Sense to Me Until I Built These Two Scripts

I’m still new to shell scripting, and for a while it felt confusing , not because nothing worked, but because I didn’t clearly understand what was happening

Most of my learning hasn’t come from memorizing commands. It came from writing scripts, breaking them, Googling errors, reading docs, and slowly understanding why something works instead of just copy pasting it.

This week, I worked on two shell scripting tasks, and for the first time, shell scripting started to feel a bit more logical.


Task 1: User Account Management , Where Things Felt Under Control

The first task was to write a script that manages users and tasks like creating users, deleting users, resetting passwords, listing users, showing user info, and printing a help menu.

At first, the list of features looked intimidating. But once I started coding, I realized the task wasn’t complex it was mostly the same idea repeated safely.

The commands themselves weren’t the hard part.
I Googled things like “check if user exists in bash”, read man useradd, tested commands manually, and slowly got comfortable.

What confused me initially was when the script should stop and when it should continue.
I was running commands assuming everything would work, which isn’t a great approach for system level scripts.

The moment things clicked was when I started adding checks before actions:

if id "$username" &>/dev/null; then
    echo "User already exists"
    exit 1
fi

This simple check changed how I approached the whole script. Instead of blindly running commands, the script first asks the system a yes/no question.

After that, every feature followed the same flow: take input → check something → perform the action → show a message.

Once I saw this pattern, the task stopped feeling scary and started feeling structured.


Task 2: Automated Backups ( Where I Actually Got Stuck)

The second task was to back up a directory, add timestamps, keep only the last three backups, delete older ones, and automate everything using cron.

Creating backups was straightforward.
Adding timestamps was also fine after a bit of trial and error.

The part that really slowed me down was backup rotation, especially the line:

“Remove old backups if more than 3 exist.”

At first, I kept thinking in terms of commands, and that wasn’t helping.
What helped was stepping back and thinking about the idea, not the syntax.

Instead of code, this is how I started thinking about it:

1. List all backup directories
2. Sort them by time
3. Count how many backups exist
4. If count > 3:
     remove the oldest ones
     keep only the latest 3

Once I understood this logic, turning it into Bash became easier after a bit of googling and stack overflow.
This was the point where I realized that most “hard” shell scripting problems are actually thinking problems, not Bash problems.


Cron: Simple Tool, but easy to mess up

After the backup script worked manually, I scheduled it with cron and it failed.

At first I thought cron was complicated. Later I realized the issue was my assumptions.

I assumed cron behaves like my terminal. It doesn’t.
I assumed relative paths would work. They didn’t.

Once I started using full paths and testing scripts properly before scheduling them, things worked as expected.


My Learnings From This Task

These two tasks didn’t make me confident overnight, but they did change how I think.

I have learned that:

  • shell scripting is more about logic than commands

  • it’s okay to rely on Google, docs, and examples

  • small checks make scripts safer

  • breaking problems into steps reduces confusion

I still feel like a beginner , because I am one.
But now the confusion feels more structured, and that feels like progress.


📂 Code & Repository

I’ve pushed both scripts and challenges to GitHub so everything discussed here can be explored directly:


Final Thoughts

These weren’t fancy projects.
But they forced me to slow down, think clearly, and understand what I was doing instead of just copying commands.

That’s the kind of learning I want to continue