Understanding the Shebang (#!/usr/bin/env bash)
The first line of almost every Bash script looks like this:
#!/usr/bin/env bash
This line is called the shebang (also written hashbang, from the two characters # and !). It is a message from the script to the operating system: "when someone runs this file as a program, use this interpreter to read it." Without it, the system has no reliable way to know whether a text file contains Bash, Python, Perl or something else. In this lesson you will learn how the system uses the line, what each part means, and which of the two common forms to choose.
Understanding the Concept
A script is only a text file. A text file cannot run by itself; some program has to read its lines and carry them out. That program is the interpreter. For a Bash script the interpreter is bash, for a Python script it is python3, and so on.
When you type ./hello.sh, you do not name an interpreter, so the system needs another way to find out which one to use. The answer is the shebang line. It must be the very first line of the file, and the first two bytes of the file must be exactly # followed by !. You can confirm this on any script:
head -c 2 hello.sh
Output:
#!
Everything after those two characters, up to the end of the line, tells the system which program to start.
How It Works
The work is done by the operating system's kernel, not by Bash. The steps are:
- You type
./hello.shand press Enter. Bash asks the kernel to execute the file (the system call is namedexecve). - The kernel opens the file and reads its first bytes. If they are
#!, the kernel knows this is a script and not a compiled program. - The kernel reads the rest of the first line. The first word is the path of the interpreter, and anything after it is treated as one optional argument.
- The kernel starts the interpreter and passes it the path of the script as an argument, followed by any arguments you typed.
So the system turns your command into something like this:
/usr/bin/env bash ./hello.sh arg1 arg2
Because the interpreter receives the script's path, it can open the file and read its lines. Inside the script, $0 holds that path and $1, $2 hold the arguments you typed:
#!/usr/bin/env bash
echo "args: $# first=$1"
Saved as ar.sh and made executable, running it gives:
./ar.sh one two
args: 2 first=one
For the interpreter, the shebang line is simply a comment, because it begins with #. Bash ignores it when it reads the file, which is why the line does no harm.
Syntax
#!interpreter-path [optional-argument]
#!are the first two characters of the file.interpreter-pathis the path to the program that should run the script. It must be a full path, because the kernel does not searchPATH.- A single optional argument may follow, separated by spaces. Everything after the first space is passed as one string.
- A space between
#!and the path is allowed by most systems (#! /bin/bash), but the form without a space is the usual one. - The line must end with a normal line feed. Windows line endings break it, as shown later.
The Two Common Forms
Two forms of the shebang are widely used for Bash.
A fixed path:
#!/bin/bash
This tells the kernel to run exactly /bin/bash. It is short and predictable. Its weakness is that Bash does not live at /bin/bash on every system. On some systems it is installed in another directory, and on macOS the file at /bin/bash is an old version (3.2), while a newer one installed with Homebrew lives elsewhere.
Search with env:
#!/usr/bin/env bash
Here the interpreter is /usr/bin/env, a small program whose job is to run another program found by searching the directories in PATH. The kernel starts env and gives it bash and the script path. Then env looks for bash in PATH and starts the first one it finds. The path /usr/bin/env exists on virtually every Linux and macOS system, so the line works where Bash is installed in an unusual place.
You can see which Bash env would pick with:
command -v bash
Output (the path depends on the system):
/usr/bin/bash
The env form follows the user's PATH. If a person has installed a newer Bash and placed it first in PATH, the script uses that one. This flexibility is also its trade-off: the script runs whichever bash comes first. To show this, this test puts a fake bash at the front of PATH:
mkdir -p fake
printf '#!/bin/sh\necho "fake bash ran"\n' > fake/bash
chmod +x fake/bash
PATH="$PWD/fake:$PATH" ./hello.sh
Output:
fake bash ran
The script was started by the program named bash that appeared first in PATH. In a normal setup that is the real Bash, but the experiment shows why a script that must be tightly controlled, such as one that runs with administrator rights, is safer with a fixed path.
Basic Example
Create a file named version.sh with this content:
#!/usr/bin/env bash
echo "Running under: $BASH_VERSION"
Make it executable and run it:
chmod +x version.sh
./version.sh
Output (your version number will differ):
Running under: 5.2.21(1)-release
BASH_VERSION is a variable that Bash itself sets, so seeing a value proves that the script was handed to Bash.
Understanding the Example
./version.shstarts the kernel's script handling.- The kernel sees
#!/usr/bin/env bash, so it starts/usr/bin/envwith the argumentsbashand./version.sh. envsearchesPATH, findsbash, and starts it with./version.shas its argument.- Bash reads the file. The first line is a comment. The second line prints the message, with
$BASH_VERSIONreplaced by the version number.
The Shebang Selects the Interpreter
Because the shebang chooses the program, it also decides which language rules apply. The same file text can behave differently under a different interpreter. Here is a script that declares sh instead of Bash but uses Bash-only syntax:
#!/bin/sh
echo "BASH_VERSION=[$BASH_VERSION]"
if [[ 5 -gt 3 ]]; then echo "bigger"; fi
Saved as shx.sh and made executable, running it directly on a system where /bin/sh is Dash (Debian and Ubuntu) gives:
./shx.sh
BASH_VERSION=[]
./shx.sh: 3: [[: not found
Dash has no [[ ... ]] and does not set BASH_VERSION. The shebang sent the script to the wrong interpreter. On a system where /bin/sh is a link to Bash, the same script could behave differently, which is why a Bash script should name Bash.
Now run the same file by naming the interpreter yourself:
bash shx.sh
BASH_VERSION=[5.2.21(1)-release]
bigger
When you run bash shx.sh, the shebang is ignored. You chose the interpreter on the command line, and the first line is just a comment. This is an important rule: the shebang matters only when the file is executed directly. The next two lessons explain how to make a file executable and how to run a script in different ways.
Using the Shebang with Other Languages
The shebang is not specific to Bash. Any interpreter works, which is why the line is the standard way to make scripts in many languages directly runnable:
#!/usr/bin/env python3
print("Hello from Python")
Saved as p.py and made executable, ./p.py prints:
Hello from Python
The same idea works for Perl, Ruby, Node.js and any other language that reads a program from a file.
Passing an Option in the Shebang
Sometimes a script should always run with an option, for example Bash's -x, which prints each command as it runs. With a fixed path this works directly:
#!/bin/bash -x
echo hi
Output:
+ echo hi
hi
The lines starting with + are the trace that -x produces. With env, the same attempt fails on Linux, because everything after the first space is passed as one argument. env then looks for a program named bash -x, which does not exist:
#!/usr/bin/env bash -x
echo hi
Output:
/usr/bin/env: 'bash -x': No such file or directory
/usr/bin/env: use -[v]S to pass options in shebang lines
The message suggests the solution: the -S option of env means "split this string into separate arguments".
#!/usr/bin/env -S bash -x
echo hi
Output:
+ echo hi
hi
The -S option exists in GNU env (version 8.30 and later), and also in the env of macOS and FreeBSD. On older systems it may be missing. If you need to be sure, set the option inside the script instead, with the set -x command, which a later lesson explains.
Messages You May See
A broken shebang line produces recognizable errors. Each of these was produced with a one-line script.
The interpreter named by env is not installed:
#!/usr/bin/env nosuchprog
echo hi
/usr/bin/env: 'nosuchprog': No such file or directory
The exit status is 127.
A fixed path that does not exist (the script below is saved as m.sh):
#!/no/such/bash
echo hi
bash: ./m.sh: cannot execute: required file not found
The exit status is again 127. The file itself exists, which is why the wording says that a required file is missing: it is the interpreter. Older systems print bad interpreter: No such file or directory.
Windows line endings:
A file saved with Windows line endings ends each line with a carriage return and a line feed. The shebang then names the program bash followed by an invisible carriage return, and the system cannot find it:
/usr/bin/env: 'bash\r': No such file or directory
/usr/bin/env: use -[v]S to pass options in shebang lines
The fix is to save the file with Unix (LF) line endings.
What Happens Without a Correct Shebang
Two cases look similar but behave differently:
- If the line is missing, or does not start with
#!in the first two bytes (for example a space comes first), the kernel cannot use it. When you run the file from Bash, Bash falls back to running the file with itself. This works on the surface, but it depends on which program started the file. - Only the first line counts. A second line such as
#!/bin/zshfurther down is an ordinary comment:
#!/usr/bin/env bash
# not a shebang
#!/bin/zsh
echo "BASH_VERSION=$BASH_VERSION"
The script runs under Bash, and the later #! line is ignored, because the kernel read only line one.
Practical Applications
A shebang makes a script behave like a normal command. A small tool that reports disk usage for a directory, with no extension, can sit in a folder that is listed in PATH:
#!/usr/bin/env bash
# dirsize - print the total size of a directory.
target=${1:-.}
du -sh "$target"
Because of the shebang, the user does not need to know that the tool is a Bash script. They type dirsize ~/Documents, and the system starts Bash behind the scenes. The same approach is used for scripts written in other languages, so users can run a tool without knowing which language it is written in.
To check the shebang of any script, print its first line:
head -n 1 dirsize
#!/usr/bin/env bash