What is a shell and shell scripting? A beginner's guide
Learn the difference between a terminal, a shell, and a command, meet the most common shells, and write your first shell script step by step, with good habits from the start.
Beginner$ echo $SHELL▍Every time you type a command into a black screen and something happens, there’s a program working behind the scenes: the shell. And when you put several commands in a file for the computer to run on its own, that’s a shell script. In this article, we’ll look at each of these pieces and write your first script from scratch.
Terminal, shell, and command: who’s who?
These three words are often used as if they meant the same thing, but they don’t:
- Terminal: the window where you type. It just displays text and receives what you write.
- Shell: the interpreter running inside that window. It reads what you typed, makes sense of it, and tells the system to run it.
- Command: the instruction you give, like
ls(list files) orpwd(show the current folder).
An analogy: the terminal is a restaurant’s counter, the shell is the waiter who takes your order to the kitchen, and the command is the order itself.
To find out which shell you’re using on Linux or macOS:
echo $SHELL
The most common shells
| Shell | Where you’ll find it | Highlight |
|---|---|---|
sh |
Virtually every Unix and Linux system | The classic shell; scripts written for it run almost anywhere |
bash |
Default on many Linux distributions | The most used in scripts and tutorials |
zsh |
Default on macOS since Catalina | Highly customizable; compatible with most of bash |
fish |
Installed separately | Built-in suggestions and colors; syntax differs from bash |
| PowerShell | Windows (also runs on Linux and macOS) | Works with objects instead of plain text |
In this article we’ll use bash, because it’s what you’ll run into most on servers and in pipelines. If you’re on macOS, the examples work there too, and you can read more about zsh here.
How a command is put together
Most commands follow the format command + options + arguments:
ls -l /etc
lsis the command (list).-lis an option: it changes the behavior (here, a detailed format)./etcis the argument: what the command acts on.
Pipes and redirection
The pipe | connects the output of one command to the input of another, like a pipe between two machines. Meanwhile, > and >> send output to a file:
ls /etc | wc -l # counts how many items are in /etc
echo "first line" > notes.txt # creates (or OVERWRITES) notes.txt
echo "second line" >> notes.txt # appends to the end, without erasing
cat notes.txt
Careful: > wipes out the file’s previous content. When in doubt, use >>.
Your first shell script, step by step
Create a file called hello.sh with this content:
#!/usr/bin/env bash
# hello.sh — my first script
name="${1:-world}" # uses the first argument, or "world"
echo "Hello, $name!"
if [ "$name" = "world" ]; then
echo "Tip: run ./hello.sh YourName"
fi
for number in 1 2 3; do
echo "Counting: $number"
done
What each part does:
#!/usr/bin/env bash: the shebang. It tells the system which interpreter should run the file (here, bash).# ...: comments. The shell ignores them; they’re for humans.name="...": creates a variable. Watch out: no spaces around the=.$1: the first argument passed to the script.${1:-world}means “use$1, orworldif it’s empty.”echo: prints text to the screen.if: runs something only if the condition is true.for: repeats a block for each item in the list.
Now make it executable and run it:
chmod +x hello.sh # makes the file executable
./hello.sh # runs with no argument
./hello.sh Maria # runs passing "Maria" as $1
The ./ means “the file is in this folder.” Without it, the shell only looks for the command in the system’s folders.
A useful example: backing up a folder
This script compresses a folder into a .tar.gz file with the date in its name and saves it to ~/backups, inside your home folder. It only creates files; it doesn’t delete anything:
#!/usr/bin/env bash
# backup.sh — compresses a folder with the date in the file name
set -euo pipefail
source_dir="${1:-}"
if [ -z "$source_dir" ] || [ ! -d "$source_dir" ]; then
echo "Usage: $0 <existing-folder>"
exit 1
fi
dest="$HOME/backups"
mkdir -p "$dest"
date_str="$(date +%Y-%m-%d_%H-%M-%S)"
name="$(basename "$source_dir")"
archive="$dest/${name}_${date_str}.tar.gz"
tar -czf "$archive" -C "$(dirname "$source_dir")" "$name"
echo "Backup created: $archive"
Use it like this: ./backup.sh ~/Documents. The result will be something like ~/backups/Documents_2026-11-24_22-54-10.tar.gz.
Good habits from day one
set -euo pipefail: makes your script safer.-estops the script at the first error;-uraises an error if you use a variable that doesn’t exist (catching silent typos);-o pipefailmakes sure an error in the middle of a pipe doesn’t go unnoticed.- Quote your variables: write
"$archive", not$archive. Without quotes, a name with a space, likeMy Documents, becomes two words and the command breaks. - Comment anything that isn’t obvious: your future self will thank you.
- Use ShellCheck: a free tool that analyzes scripts and points out common mistakes. It works in the terminal (
shellcheck backup.sh) and as an extension for editors like VS Code.
Shell script or Python?
A rule of thumb: use a shell script to glue together commands that already exist (copying files, running tar, calling git or docker) for short tasks. Prefer Python when the script starts to have complex logic, lots of conditions, JSON handling, calculations, or needs to run the same way on Windows, Linux, and macOS. If your bash script has grown past a few dozen lines and is hard to read, that’s a good sign to switch.
Try it yourself
- Run
echo $SHELLand find out which shell you’re using - Create
hello.sh, runchmod +x, and run it with and without an argument - Add a
forloop tohello.shthat goes through three fruit names - Use
>and>>to create a file with three lines and check it withcat - Run
backup.shon a test folder and list the result withls ~/backups - In bash, run
folder="my folder", thenmkdir "$folder", and comparels "$folder"withls $folder(no quotes) - Install ShellCheck and run it on both of your scripts
Next steps
If you’ve done the same task in the terminal three times, it already deserves to become a script.