Shebang

Also known as: hashbang

On this page 6

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.

RequirementDetailFailure if missing
Execute permissionSet with chmod +x scriptPermission denied
First two bytesExactly #!, with no byte order markExec format error or shell fallback
Valid interpreter pathAbsolute path to an existing executableBad interpreter: no such file
Unix line endingsLF, not CRLFInterpreter name ends with a carriage return
Line length limit255 bytes before Linux 5.1, 511 bytes sinceTruncated interpreter arguments
Requirements for shebang execution

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 lineInterpreter selectedTypical use
#!/bin/shSystem POSIX shellPortable scripts without Bash extensions
#!/bin/bashBash at a fixed pathLinux system scripts using Bash features
#!/usr/bin/env bashFirst Bash found in PATHScripts shared across Linux, macOS and BSD
#!/usr/bin/env python3First Python 3 in PATHPython tools that respect virtual environments
#!/usr/bin/env -S awk -fawk with a split argumentInterpreters that need several options
Typical shebang lines

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.

AspectAbsolute pathLookup through env
PredictabilityAlways the same binaryDepends on the caller’s PATH
PortabilityBreaks if the path differsWorks wherever the program is in PATH
SecurityNot affected by PATH manipulationA malicious PATH entry can hijack execution
Multiple argumentsOne argument on LinuxSeveral arguments with env -S
Virtual environmentsIgnoredRespected
Absolute path versus env

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; dos2unix or editor settings fix them.
  • Running a script explicitly with sh script.sh ignores 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 sudo rules instead.
  • Mount options such as noexec on /tmp block execution regardless of the shebang.
  • Missing shebang makes the calling shell guess, which typically means the script runs under /bin/sh with 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.