Skip to content

shebang (#!)

In Python, a shebang is the character sequence (#!) placed on the first line of a script file, followed by the path to the interpreter that should run the file. On Unix-like systems, the operating system reads this line when the file is executed and uses the named interpreter to launch the script. For this to work, the file must have the executable permission bit set.

The shebang is also called a hashbang or sharp-bang line, after the names of its two characters.

Python ignores the shebang at runtime because it begins with #, which marks the start of a comment. The line is meaningful only to the kernel and to script launchers. The first two bytes of the file must be # and ! with no leading whitespace or byte order mark, or the kernel will not recognize the file as a script.

A typical Python shebang points to the interpreter through the env utility, which makes the script portable across systems where Python is installed in different locations:

Language: Python
#!/usr/bin/env python3

Syntax

A shebang line follows this form:

Language: Python Syntax
#!<interpreter-path> [optional-arguments]

The interpreter path must be absolute. Common variants for Python scripts include:

  • #!/usr/bin/python3 calls the system Python 3 interpreter at a fixed location.
  • #!/usr/local/bin/python3 calls a Python installed under /usr/local.
  • #!/usr/bin/env python3 searches the user’s PATH for python3 and runs the first match. This form is the most portable across Linux, macOS, and BSD systems.
  • #!/usr/bin/env python3.14 selects a specific Python version when several are installed.

Anything after the interpreter path on the same line is passed as a single argument on Linux. Other Unix variants split arguments differently, so portable scripts pass at most one flag.

Example

The following script uses a shebang so that it can be executed directly from the shell:

Language: Python Filename: greet.py
#!/usr/bin/env python3
"""Print a friendly greeting and the running Python version."""
import sys

major = sys.version_info.major
minor = sys.version_info.minor
print(f"Hello from Python {major}.{minor}")

Save the file as greet.py and mark it executable with chmod +x. You can then run the script directly without naming the interpreter on the command line:

Language: Shell
$ chmod +x greet.py
$ ./greet.py
Hello from Python 3.14

The shell reads the shebang, locates python3 on the user’s PATH, and passes greet.py to it for execution.

Tutorial

Executing Python Scripts With a Shebang

In this tutorial, you'll learn when and how to use the shebang line in your Python scripts to execute them from a Unix-like shell. Along the way, you'll run custom scripts written in your domain-specific language interpreted by Python.

intermediate best-practices

For additional information on related topics, take a look at the following resources:


By Martin Breuss • Updated May 6, 2026 • Reviewed by Leodanis Pozo Ramos