A shebang is the character sequence #! followed by an interpreter path on the first line of a script, such as #!/bin/bash, which tells the operating system which program must execute the file. On Linux and other Unix-like systems, the shebang lets a script run like a compiled program: a user types ./backup.sh instead of bash backup.sh, and the kernel itself starts the correct interpreter.
The name combines the sharp sign and the bang, the traditional name of the exclamation mark; the synonym hashbang describes the same two characters. Although the line is only a few bytes long, it decides which shell or language runtime interprets the script, which options it receives and whether the script behaves the same on another machine.
How the kernel processes a shebang line
The shebang is handled by the kernel, not by the shell. When a process calls execve() on a file, the Linux kernel reads the first bytes; if they are #!, the binfmt_script handler parses the rest of the line as an interpreter path plus an optional argument. The kernel then executes that interpreter and passes the script path as an argument, followed by the original arguments.
For a file /usr/local/bin/report starting with #!/usr/bin/python3 -u, running report --daily becomes the call /usr/bin/python3 -u /usr/local/bin/report --daily. The interpreter must then treat the first line as a comment, which is why the hash character works: it starts a comment in shells, Python, Perl, Ruby and many other languages.
Several conditions must hold for this mechanism to work, as the following table shows.
| Requirement | Detail | Failure if missing |
|---|---|---|
| Execute permission | Set with chmod +x script | Permission denied |
| First two bytes | Exactly #!, with no byte order mark | Exec format error or shell fallback |
| Valid interpreter path | Absolute path to an existing executable | Bad interpreter: no such file |
| Unix line endings | LF, not CRLF | Interpreter name ends with a carriage return |
| Line length limit | 255 bytes before Linux 5.1, 511 bytes since | Truncated interpreter arguments |
Common shebang forms and their meaning
The choice of shebang determines portability and behavior. A script that uses Bash features such as arrays or [[ ]] tests must not declare /bin/sh, because on Debian and Ubuntu /bin/sh is dash, a minimal POSIX shell that rejects those constructs.
| Shebang line | Interpreter selected | Typical use |
|---|---|---|
#!/bin/sh | System POSIX shell | Portable scripts without Bash extensions |
#!/bin/bash | Bash at a fixed path | Linux system scripts using Bash features |
#!/usr/bin/env bash | First Bash found in PATH | Scripts shared across Linux, macOS and BSD |
#!/usr/bin/env python3 | First Python 3 in PATH | Python tools that respect virtual environments |
#!/usr/bin/env -S awk -f | awk with a split argument | Interpreters that need several options |
The env form delegates the lookup to the env utility, which searches the PATH environment variable. This makes scripts portable to systems where Bash lives in /usr/local/bin or /opt/homebrew/bin, and it lets Python scripts pick up an activated virtual environment.
The mechanism dates back to Unix systems of the early 1980s and is now supported by virtually every Unix-like kernel, although details differ. Linux accepts an interpreter that is itself a script only in limited cases, while macOS and the BSD family apply their own length limits and argument rules. POSIX does not standardize the shebang, so the behavior of anything beyond a single absolute path plus one optional argument should be treated as platform-specific. On Linux, the related binfmt_misc facility extends the same idea to files identified by magic bytes or extensions, which allows, for example, Java archives or foreign-architecture binaries to start through a registered interpreter.
Absolute paths compared with env lookup
Neither form is universally better; the choice is a trade-off between predictability and portability. Security-sensitive and system scripts usually prefer absolute paths, while developer tools favor env.
| Aspect | Absolute path | Lookup through env |
|---|---|---|
| Predictability | Always the same binary | Depends on the caller’s PATH |
| Portability | Breaks if the path differs | Works wherever the program is in PATH |
| Security | Not affected by PATH manipulation | A malicious PATH entry can hijack execution |
| Multiple arguments | One argument on Linux | Several arguments with env -S |
| Virtual environments | Ignored | Respected |
A frequent surprise concerns argument splitting. Linux passes everything after the interpreter path as one single argument, so #!/usr/bin/env bash -e tries to run a program whose name is exactly bash -e, including the space. The -S option of GNU coreutils env splits that string, but it is not available on every older system.
Shebang pitfalls in daily administration
Most shebang problems surface as confusing error messages that do not mention the first line directly. The following points cover the cases administrators meet most often.
- CRLF line endings from Windows editors produce errors such as bad interpreter with a hidden carriage return;
dos2unixor editor settings fix them. - Running a script explicitly with
sh script.shignores the shebang entirely, so Bash-specific code may fail even though the shebang says Bash. - Set-user-ID scripts are ignored by the Linux kernel for security reasons, so privileged scripts need
sudorules instead. - Mount options such as
noexecon/tmpblock execution regardless of the shebang. - Missing shebang makes the calling shell guess, which typically means the script runs under
/bin/shwith unpredictable results.
Linters such as ShellCheck read the shebang to decide which shell dialect to analyze, so an accurate first line also improves static analysis and catches Bash-only syntax in scripts declared as POSIX shell.
Understanding it simply
A shebang is like a note on the first page of a document that says which language it is written in. When you hand the file to the computer and ask it to run, the computer reads that note and calls the right translator, such as Bash or Python, to read the rest.
Without the note, the computer has to guess, and a wrong guess means the instructions are misread. One correct first line makes a script run the same way every time, no matter who starts it.
Applied in practice
A system administrator writes a nightly backup script on an Ubuntu server that uses Bash arrays to list directories. The script starts with #!/bin/sh, and when cron runs it at 02:00, it fails with a syntax error, because on Ubuntu /bin/sh points to dash, which does not support arrays. During interactive tests the administrator had always started it with bash backup.sh, which hid the mismatch until the first scheduled run.
The administrator changes the first line to #!/usr/bin/env bash, adds set -euo pipefail on the next line instead of passing options in the shebang, marks the file executable with chmod 750 and validates it with ShellCheck. The backup runs successfully the following night, and the same file later works unchanged on a macOS build machine where a newer Bash is installed in a different directory.