Articles
Published · November 18, 2026

The #! Shebang Line: How a Text File Becomes a Script on Mac

A shebang line is #! plus an interpreter on line one. Add chmod and a plain text file runs as a script on your Mac. Here is how it works, and why it fails.

Also in: Español中文हिन्दी

Quick answer

A shebang line is the first line of a text file, starting with #! and followed by the program that should run the file. Add that line, make the file executable, and your Mac treats the text file as a script. Apple's Terminal guide describes it the same way: the characters # and !, called a shebang, followed by a reference to the shell the script should be run with.

Here is the whole recipe for a file called hello.sh:

#!/bin/sh
echo "Hello from a text file"
chmod 755 hello.sh
./hello.sh

The first command marks the file as executable. The second runs it by its path. Apple's guide uses chmod 755 for this step and says shell scripts must be executable to run. The file name doesn't matter here. .sh is just a habit.

How it actually works

What the system does with line one

When you run a text file as a program, the loader reads the rest of its first line as an interpreter directive, as the Shebang (Unix) article) puts it. It starts that interpreter and hands it the script's path. A file that begins with #!/bin/sh at path/to/script ends up running as /bin/sh path/to/script.

The two bytes #! (0x23 0x21) act as a magic number that the exec family of functions looks for. If they are there, the file is not treated as a plain text document, it is treated as input for the named interpreter.

One detail trips people up: the line is only used when the file itself is executed. Run sh hello.sh and the first line is ignored, because you named the interpreter yourself. Run ./hello.sh and the line decides everything.

Why people write /usr/bin/env

A shebang needs an absolute path, and the right path differs between machines. Python might live in one place on your Mac and somewhere else on a colleague's. The common workaround points the shebang at env and lets it find the interpreter:

#!/usr/bin/env python3
print("Hello, World!")

The env page on Wikipedia explains that env can be used in the hashbang line so the interpreter is looked up through your PATH. It calls execvp, which locates the program and launches it. /usr/bin/env is the full path of env itself, and it does not alter the environment in this use.

The trade-off is real: a script that uses env is more portable, but the same page notes it also raises the risk that the wrong interpreter is selected, since the search covers every directory on your PATH. If you have two Python installs, the first one found wins. Hard-coding a path gives you certainty and costs you portability. For a script you keep on one Mac, either is fine. For one you share, env is usually the safer bet.

When to use it (and when to skip it)

Limits worth knowing

  • One argument. On Linux and Cygwin, everything after the first space is passed as a single argument. #!/usr/bin/env python3 -c hands env the string python3 -c, not two separate words. Some env versions support -S to split the string (FreeBSD 6.0 added it, GNU coreutils has it since 8.30), but it only works where env supports it. For portable scripts, stick to one option or none.
  • The interpreter must be a binary on macOS. On Darwin-derived systems such as macOS, the file named after #! must be an executable binary. It cannot itself be a script.
  • Naming the interpreter skips the line. That's handy for testing, and confusing when a script behaves differently depending on how you started it.

Why a script silently fails

Two invisible problems cause most "it worked yesterday" reports: A UTF-8 byte order mark before the #! stops the interpreter from running, because the first bytes are no longer #!. And DOS line endings, a carriage return after the shebang line, can make some systems fail, since the interpreter name now ends with a stray character. Save scripts as UTF-8 without a BOM and with Unix line endings.

This is also why scratch scripts pile up. If you keep small helpers around, our piece on scratch files and snippets covers pruning them. For a worked example of a small script that reads a text list, see the plain-text file and a small script approach to a gift draw.

When to skip it

If you run a command once, skip the shebang and the chmod. Type python3 script.py or sh script.sh and move on. The shebang earns its place when a file is something you run again and again, from different folders, or hand to someone else.

How Carets fits in

A script is a plain text file, which is exactly what Carets is built for. Carets is a native notes and code editor for iPhone, iPad, and Mac, so you can write and fix a shebang line wherever you are. Three features matter for this topic:

  • Syntax highlighting for code and config files, so a comment, a string, and the #! line are easy to tell apart.
  • Plain text stays plain text. Your files remain portable, with no hidden formatting that could sneak in ahead of the #!. If you want background on what plain text files are, see our note on plain text files.
  • Files, projects, and tags to keep small scripts grouped with the notes that explain them.

To be clear about scope: Carets edits the file, but chmod still happens in Terminal on your Mac, and you should check line endings and encoding when a script moves between systems.

Get Carets on the App Store for iPhone, iPad, and Mac.

Frequently Asked Questions

Do I need a .sh extension for a script to run on Mac?

No. The shebang line and the executable permission decide how a file runs, not its name. Apple's guide uses .sh only as the example name.

Why does my script fail after I edited it?

Two common causes are a byte order mark before #! and DOS (CRLF) line endings after the shebang line. Both can stop the interpreter from being found. Save as UTF-8 without a BOM, with Unix line endings.

Can I pass options after env in a shebang?

Not reliably. Linux passes everything after the first space as one argument. env -S splits the string, but it only works where env supports it, so keep to one option or none for portability.

What happens if I run the script with its interpreter by name?

The shebang line is ignored. sh script.sh uses sh no matter what the first line says, while ./script.sh uses the line.