Skip to the workspace
Pebellslearn by building
Your First Bash Script  ›  Lesson

Anatomy of a Script: Shebang and Comments

Bash Script Anatomy 12 minA Bash script starts with a shebang line that names its interpreter, then lists commands in order, with # starting comments that Bash ignores.

shell bash / Anatomy of a Script: Shebang and Comments

Explore each scene at your own pace.

01 · The rule

The rule

Anatomy of a Script: Shebang and Comments

A Bash script starts with a shebang line that names its interpreter, then lists commands in order, with # starting comments that Bash ignores.

Inspect the interpreter declaration and separate executable commands from comments.

Open any script from a real project and the first line looks like line noise: #!/usr/bin/env bash. Below it come notes starting with #, and then ordinary commands. Once you can name these three kinds of line, every script becomes readable.

The rule

Shebang first, commands in order, and # starts a comment.

Here is backup.sh, now written as a proper script:

#!/usr/bin/env bash
# backup.sh: pack the notes folder into ~/backups

mkdir -p ~/backups                        # make sure the target exists
tar -czf ~/backups/notes.tar.gz ~/notes   # pack and compress
echo "Backup done"

The shebang. #! must be the very first two characters of the file. When you run the file directly, the system reads this line to learn which interpreter to start. #!/bin/bash points at one fixed location; #!/usr/bin/env bash asks env to find bash on your PATH, which works even where Bash lives somewhere else (as on many macOS setups with a newer Bash installed).

Comments. A # at the start of a word begins a comment that runs to the end of the line. Bash ignores it. Use a header comment to say what the script does, and short end-of-line comments to explain why a line exists. But a # inside quotes, or in the middle of a word, is ordinary text:

echo "Total # of files"   # prints: Total # of files
echo price#10             # prints: price#10

Commands. Everything else runs top to bottom. Blank lines are ignored. A line ending in a backslash \ continues on the next line, which keeps long commands readable. Two short commands can share a line when separated by ;.

Scene 1 of 5
Hover to wake the cannons. Drag to move, or use arrow keys when focused. Press Enter to fire a shot.