How to Write a Simple Shell Script for Beginners 2026

If you have typed how to write a simple shell script for beginners into a search box and landed here, the answer is short. You create a plain text file, put a shebang line at the top, add a few commands, make the file executable with chmod +x, and run it with ./hello.sh. The whole loop takes about five minutes, and you need nothing more than a terminal and a text editor.

That is the entire idea. A shell script is just a text file holding a list of commands your terminal would otherwise make you type one by one. Once you have the create, save, run and edit cycle working, scripting stops being mysterious and starts being a shortcut.

On macOS and Linux the shell is already there. On Windows you will want WSL or Git Bash first, and I cover that in the setup section below.

What You Need

Four things, and three of them are already installed.

  • A terminal. Terminal on macOS, GNOME Terminal or Konsole on Linux, or your Windows Terminal once WSL is set up.
  • A Bash-compatible shell. macOS ships both zsh (your default) and an older Bash at /bin/bash. Most Linux distributions ship Bash as the default.
  • A text editor. nano comes in the terminal and is the friendliest for a first script. Vim, VS Code or anything else works fine too.
  • File permissions. Not software, just the idea that a file can carry a flag saying “run me”. chmod +x sets that flag.

Which shell you land in matters less than people think, but it does explain a lot of tutorials disagreeing with each other.

ShellWhat it isWhere you meet it
shThe original POSIX shell, strict and portableStartup files, minimal containers, scripts that must run anywhere
bashBourne Again Shell, the common default on LinuxAlmost every beginner tutorial and sysadmin script
zshModern shell with better completion and promptsThe default you type into on macOS
fishFriendlier syntax, deliberately not bash-compatiblePeople who outgrew bash and installed it deliberately
kshLegacy shell from the 1980sOld Solaris and AIX servers still in production

Use echo $SHELL to see which one opened your terminal, and bash --version to confirm Bash exists even if it is not your default. If you want to follow a tutorial written for Bash, run the commands through Bash and you will not trip over zsh differences.

Step-by-Step: How to Write a Simple Shell Script

1. Open a terminal and choose a working folder

Open your terminal. On macOS, Terminal is under Applications and Utilities; on Linux it is usually in the dock already.

Decide where the script lives. A dedicated folder keeps your home directory clean, and mkdir -p never complains if the folder is already there.

pwd
mkdir -p ~/shell-scripts
cd ~/shell-scripts
pwd

The second pwd should print your new folder path. That output is your confirmation that step one worked.

2. Create the script file

Create the script file

Open a new empty file called hello.sh. The .sh extension is a convention rather than a rule, but it keeps your scripts recognisable.

nano hello.sh

A blank screen with a row of shortcuts at the bottom means you are in nano. If the file already exists, nano opens it instead of wiping it, so do not be surprised if your commands are still there.

3. Add the shebang and first commands

The shebang is the first line of the script and tells the operating system which interpreter should read the rest of the file. Without it, the system guesses, and the guess is wrong often enough to waste an afternoon.

#!/bin/bash

# A greeting script that takes a name, or falls back to a default
name="world"

if [ $# -eq 0 ]; then
  name="world"
else
  name=$1
fi

echo "Hello, $name!"
echo "Today is $(date +%A)."

Type or paste that in, character for character. Two rules cause most of the errors beginners hit here: no spaces around the equals sign when you assign a variable, and single spaces inside the square brackets.

You can see the two variables do different jobs. name is a variable you made up, and $# is a built-in that holds the number of arguments passed to the script.

4. Save the script and make it executable

In nano, save with Ctrl + O, confirm the filename, then exit with Ctrl + X. The bottom of the screen confirms each step, so a script that “did not save” usually means the shortcut never registered.

Now set the executable bit, which is the step people forget and then meet as a permission denied error.

chmod +x hello.sh
ls -l hello.sh

The listing shows -rwxr-xr-x. Those first ten characters are the permissions, and the third one being x for the owner is what chmod +x just added.

You do not need that flag to run the script. bash hello.sh hands the file to Bash directly and works either way, which is a handy trick while you are still editing.

5. Run the script and test its output

Run it with an argument, then without one. The output should change, which proves the script is reading what you pass it.

./hello.sh Priya
Hello, Priya!
Today is Saturday.

./hello.sh
Hello, world!
Today is Saturday.

If you see Permission denied, the executable bit did not stick. If you see No such file or directory, you are in the wrong folder, and pwd will tell you where you actually are.

Two habits pay off from day one. bash -n hello.sh checks syntax without running anything, and echo $? right after a command prints its exit code, where 0 means success.

6. Make a small useful change

Editing is where scripting actually starts. Add an hour check so the script changes its greeting in the evening, then rerun it.

hour=$(date +%H)

if [ $hour -ge 18 ]; then
  echo "Good evening, $name!"
elif [ $hour -ge 12 ]; then
  echo "Good afternoon, $name!"
else
  echo "Good morning, $name!"
fi

Run it again and confirm the line matches the time. You have just completed the loop that scripting is built on: edit, run, look at the output, fix what surprised you.

Common Mistakes

Almost every beginner error comes from one of these eight, and the error text tells you which one you hit.

MistakeWhat you seeFix
Forgot the shebangExec format error, or the file opens in a text editorPut #!/bin/bash on line one
Skipped chmodbash: ./hello.sh: Permission deniedRun chmod +x hello.sh
Spaces around the equals signname=world: command not foundWrite name="world" with no spaces
No spaces inside the brackets[: missing ] or a silent wrong resultWrite [ -f file ] with spaces
Unquoted variable in a filenameWorks until a folder is called My PhotosWrite "$file" in every command
Wrong working directoryNo such file or directoryCheck pwd, then use the full path
Unclosed quoteWaiting for more input, or unexpected EOFCount your single and double quotes
Edited but never savedYour change did nothing at allCtrl + O to save before exiting nano

The spacing rules look arbitrary because they are historical, not logical. Bash treats = and the brackets as operators with required separators, so treat them like language keywords rather than maths symbols.

One habit prevents a whole category of silent failure: add set -euo pipefail below the shebang once your script does more than a couple of lines. It stops the script on the first failed command, catches unset variables, and treats broken pipes as errors.

Windows users hit a separate wall. A .sh file will not run from PowerShell or Command Prompt, so use WSL for a real Linux environment or Git Bash for a lighter one that still gives you chmod and #!/bin/bash.

Frequently Asked Questions

Do I need chmod +x to run a shell script?

No. Running bash hello.sh hands the file to Bash directly and works with any permissions. You only need the executable bit for the ./hello.sh form, or for scripts invoked from cron, systemd, or another script. Setting it once at the start saves the confusing error later.

Which shebang should I use, bash or sh?

Use #!/bin/bash for anything you learn from today onwards, because Bash is the default on most Linux systems and has better error messages. Use #!/bin/sh only when the script must run on minimal systems with no Bash installed. Keep the shebang and the shell you test in sync.

Is shell scripting hard to learn?

The first script takes minutes. The parts that feel awkward early on are spacing rules and quoting, and both stop being confusing after a few deliberate errors. Most people write useful scripts after a weekend of practice. Treat bash like any other tool and keep scripts short while you learn.

Should I use Bash or Python for automation?

Use Bash when the job is gluing commands together: backups, log cleanup, file renames, service checks. Use Python when you need data structures, real error handling, libraries, or anything with more than a little branching. Shell is faster to type, Python is easier to grow once a script passes thirty lines.

Why does my script say permission denied?

The file is not marked executable. Run chmod +x scriptname.sh in the same folder as the file and try ./scriptname.sh again. If you prefer not to change permissions, bash scriptname.sh runs it just fine. Also check that you used chmod on the file itself and not on its parent folder.

How do I run the same script on a schedule?

Put the absolute path to your script in a crontab entry, then run crontab -e to edit it. Use full paths inside the script too, because cron runs with almost no environment set. A line such as 0 7 * * * /home/you/shell-scripts/report.sh runs it every day at seven in the morning.

Start with the script you already built. Open hello.sh, change the default name to your own, and run it without an argument so the fallback path gets exercised. That edit-test-improve loop is how to write a simple shell script for beginners, and everything after it, from loops to functions, is the same idea in a bigger file.

If you want the honest version of the learning curve, the first month is mostly about stopping yourself from being surprised by quoting. Keep scripts under thirty lines, read the error messages instead of skipping them, and shell scripting stops feeling like a separate language you have to memorise.

Leave a Comment