What is this shebang comment starting with #!
May 16, 2021 ‐ 2 min read
If you have seen an executable script for a UNIX system good chance the first line started with #!
. This is a special comment called the "shebang" and it specifies the interpreter that should be used to execute the file with.
So if you are looking at a bash script you may see #!/bin/bash
, if you are looking at a Python script you may see #!/usr/bin/python3
as shebang.
The shebang comment uses the following structure:
#![path to interpreter] [possible arguments]
You may also encounter a shebang that is pointing to the interpreter /usr/bin/env
. This is useful in case you are not sure where your preferred interpreter is located on the system that might run the script, whether it is a server in the AWS cloud or the laptop of a colleague. It makes your script portable so to say since by using /usr/bin/env
PATH is searched for the command you pass as an argument. The following example shows how this shebang with the bash
interpreter.
#!/usr/bin/env bash
In case no shebang comment is present the default interpreter used depends on the shell you are using. Bash uses itself as a fallback when no shebang is specified. While Zsh uses /bin/sh
as a fallback on the other hand, what /bin/sh
actually refers to depends on your system.
Also make sure your script has executable permissions before you execute it. You may do this with the chmod
command:
$ chmod u+x ./script.sh