Shebang Hashbang

What is a Shebang?

A shebang (also called a "hashbang") is a special sequence of characters at the beginning of a script file that tells the system which interpreter to use to execute the script.

Syntax

The shebang always starts with #! followed by the path to the interpreter:

#!/path/to/interpreter

How it works

When you run a script directly (like ./myscript.sh), the operating system reads the first line. If it starts with #!, the system uses the specified interpreter to run the script instead of trying to execute it as a binary.

Common examples

#!/bin/bash          # Bash script
#!/usr/bin/python3   # Python script  
#!/usr/bin/env node  # Node.js script
#!/usr/bin/env ruby  # Ruby script

Why use it?

Without a shebang, you'd need to manually specify the interpreter every time:

# Without shebang
bash myscript.sh

# With shebang
./myscript.sh

The shebang makes scripts self-executing and portable across different systems.


Backlinks