Writing Your First Bash Script

Write, save and run your first Bash script, and understand the shebang line, executable permissions and basic script structure.

~7 min Beginner

What is a script?

So far you have typed one command at a time. A script is a text file that contains commands. When you run it, the shell executes the commands one after another, from the top to the bottom.

Scripts make tasks repeatable. If a job takes five steps, you write them once in a script, and from then on you run one command instead of five.

Writing the script

Use any text editor. nano is a simple one that runs inside the terminal:

nano hello.sh

Type this into the file:

#!/bin/bash
# My first script
echo "Hello from my script!"
echo "Today is $(date +%A)"
echo "You are logged in as $USER"

Save with Ctrl + O then Enter, and exit nano with Ctrl + X. The .sh ending is a convention, not a requirement.

The shebang line

The first line, #!/bin/bash, is called the shebang. It tells the system which program should run the file. Here it says "use Bash, found at /bin/bash".

  • #!/bin/bash uses Bash at that exact location.
  • #!/usr/bin/env bash finds Bash wherever it is installed, so it is more portable.

The shebang must be the very first line, with nothing above it.

Comments

Any line that starts with # is a comment. The shell skips it. The shebang line is a special case, because the system reads it before Bash starts.

# Back up the documents folder
cp -r ~/Documents ~/backup    # comments can also follow a command

Write comments that explain why you did something. The code already shows what it does.

Running the script

There are two ways.

bash hello.sh

This starts Bash and tells it to read hello.sh. It works with no extra setup.

The other way turns the file into a program. Files are not executable by default, so first add the execute permission, then run it:

chmod +x hello.sh
./hello.sh

The ./ means "the file in the current folder". The shell only looks for commands in the folders listed in PATH, and your current folder is usually not one of them. Without ./, typing hello.sh gives command not found.

The complete workflow

  1. Write the script with a shebang on the first line.
  2. Make it executable with chmod +x.
  3. Run it with ./script.sh.

Variables in a script

#!/bin/bash
name="Alex"
city="London"
echo "$name lives in $city"

Remember: no spaces around =, and use double quotes when you read the variable.

A slightly bigger example

This script moves PDFs from your Downloads folder into a folder named after today's date:

#!/bin/bash
# Move PDFs into a dated folder
folder="$HOME/Downloads/pdfs-$(date +%F)"
mkdir -p "$folder"
mv "$HOME"/Downloads/*.pdf "$folder"/ 2>/dev/null
echo "Moved PDFs into $folder"

Each line uses something you already know: variables, command substitution, mkdir -p, wildcards and redirection.

Finding and fixing problems

If a script does something unexpected, run it in debug mode:

bash -x hello.sh

-x prints each line, with variables replaced, just before running it, so you can see exactly what happened.

Where to keep scripts

Put your scripts in a folder such as ~/bin and add that folder to PATH, as shown in the PATH lesson. You can then run them by name from anywhere.

Habits of good scripts

  • Always start with a shebang.
  • Comment the why, not only the what.
  • Quote your variables.
  • Use clear names such as backup_dir, not x.
  • Test on harmless data first.
Sign up to track your progress Mark lessons as read and pick up where you left off Sign up free to unlock this practice Your first practice lesson is free to try. Create an account to unlock the rest and save your progress