How to Create a Custom Bash Script for Automation

Writing the same command over and over can get boring. I’ve been there. When I first started using Linux, I didn’t know I could automate tasks. But then I learned about Bash scripts, and everything changed.

In this guide, I’ll show you how to make your own custom Bash script. I’ll explain things simply. I’ll also share why scripts are helpful and how they can save you time.

If you’re new to this, no problem. I’ll walk with you through the steps.


What Is a Bash Script?

Let’s keep it simple.

Bash is the name of a shell, or command-line program, that lets you type instructions to your computer.

A script is a small file that stores a list of those instructions.

So, a Bash script is just a file that holds several Bash commands.

Instead of typing each command one at a time, you put them into a script and run it all at once.

It’s kind of like saving a recipe. Instead of remembering all the steps, you just read the list and follow it.


Why Use a Bash Script?

For me, Bash scripts help take care of little tasks I do often—like backing up files, updating software, or cleaning up folders.

Here’s why I think they’re useful:

  • Save time: One click can run many commands.
  • Avoid mistakes: The script runs the same way every time.
  • Feel more in control: You understand what’s happening behind the scenes.

You don’t need to be a programmer to use them. If you’ve used the terminal before, you can write a Bash script.


Things You’ll Need

Before you start, make sure you have these:

  • A Linux system (Ubuntu, Debian, Fedora, etc.)
  • A text editor like nano or vim (or even a code editor like VS Code)
  • Some basic knowledge of terminal commands

That’s it. You don’t need anything fancy.


Step-by-Step: Create a Simple Bash Script

Let me show you how I made one of my first scripts.

1. Open a Terminal

On most Linux systems, you can open the terminal by pressing Ctrl + Alt + T.

2. Make a New File

Use this command:

nano myscript.sh

This opens a new file named myscript.sh using the nano editor.

3. Add the Shebang Line

The first line of your script should be this:

#!/bin/bash

This tells your system to use the Bash shell to run the script.

4. Write Your Commands

Below the shebang line, add the commands you want. Here’s a basic example:

#!/bin/bash

echo "Hello, this is my first Bash script!"
date
uptime
  • echo prints a message.
  • date shows the current date and time.
  • uptime shows how long the computer has been running.

Save the file by pressing Ctrl + O, then press Enter, and exit with Ctrl + X.

5. Make It Executable

Before you can run it, you need to give it permission:

chmod +x myscript.sh

This makes the file executable, which means you can run it like a program.

6. Run Your Script

Now, run your script with:

./myscript.sh

You should see your message, the date, and your system’s uptime.

That’s it. You just wrote and ran a Bash script.


What Can You Do With Bash Scripts?

The cool thing about Bash scripts is that they can do almost anything you can type in the terminal.

Here are a few ideas to get you started:

  • Create a backup script
    Save your files to a backup folder every day.
  • Clean up logs or temp files
    Remove old files that you don’t need.
  • Install updates automatically
    Run updates on your system at night when you’re not using it.

My Daily Backup Script (Real Example)

I use this one almost every day:

#!/bin/bash

SOURCE="/home/myname/Documents"
DEST="/home/myname/Backups"
TODAY=$(date +%F)

mkdir -p "$DEST/$TODAY"
cp -r "$SOURCE" "$DEST/$TODAY"

echo "Backup done for $TODAY"

Let’s break it down:

  • SOURCE and DEST are where the files come from and go to.
  • $(date +%F) makes a folder with today’s date (like 2025-05-07).
  • mkdir -p makes the folder if it doesn’t already exist.
  • cp -r copies everything, including subfolders.
  • The last line shows a simple message.

This saves my work without me thinking about it.


What I Learned From Writing Bash Scripts

I didn’t always get things right the first time. I made mistakes. But each time I fixed something, I learned more.

Here are a few things that helped me:

  • Keep scripts simple at first
  • Use echo to print values and test things
  • Look up each command if you don’t know what it does

I also found it helpful to compare scripts with recipes. Once I thought of scripts as simple instructions, it felt less scary.


Tips for Writing Better Bash Scripts

As you make more scripts, here are some habits I found useful:

  • Add comments
    Use # to explain what each part does.
  • Use variables
    Store values like folder paths so you don’t repeat them.
  • Handle errors
    Add checks so the script doesn’t crash if something is missing.

Here’s a quick example using those tips:

#!/bin/bash

# This script updates the system

echo "Starting update..."

if [ "$(id -u)" -ne 0 ]; then
  echo "Please run as root"
  exit 1
fi

apt update && apt upgrade -y

echo "Update complete."

This script checks if you’re root (admin) before updating the system.


Common Bash Terms

Here are a few words that might pop up as you learn Bash:

  • Variable: A name that holds a value, like USERNAME="john"
  • Loop: A way to repeat something, like checking multiple files
  • Condition: A test, like if something is true, then do something

You don’t need to memorize them now. Just keep learning as you go.


Benefits of Bash Scripting

Still wondering why you should use Bash scripts? Here are three solid reasons:

  • Consistency
    Scripts always run the same, so you avoid human errors.
  • Speed
    You can do in 2 seconds what might take you 5 minutes by hand.
  • Confidence
    You know exactly what’s happening, step by step.

It’s not about being a genius. It’s just about learning how to tell your computer what to do.


Things to Watch Out For

Here are a few common mistakes I made early on:

  • Forgetting to make the script executable
    Always run chmod +x script.sh
  • Using the wrong path
    Double-check file and folder paths.
  • Missing #!/bin/bash
    If the shebang line is missing, it may not run correctly.

Final Thoughts

Bash scripting is a quiet superpower. It may not look fancy, but it gets the job done. I use scripts almost every day now—for little things like organizing files or checking my system.

You don’t need to be an expert to start. You just need to try.

So, what do you want to automate? What boring task do you wish your computer could do for you?

Write it down, and try making a script. Keep it simple. Test it. Learn from it.

Before long, you’ll have a toolbox of scripts that make your life a little easier.

Leave a Reply