How to use the shebang

This article describes what the shebang is, and how to use it to make sure that your script files run correctly.

What is the shebang?

The shebang is a special character sequence in a script file that specifies which program should be called to run the script. The shebang is always on the first line of the file, and is composed of the characters #! followed by the path to the interpreter program. You can also specify command line options, if necessary.

For example, the following line contains the shebang, the path to the Perl interpreter, and the command line option -w:

#!/usr/bin/perl -w

The shebang is especially important for CGI script files. For example, if a file named script.cgi contains the previous shebang line, you can run it from the command line by simply typing:

./script.cgi

Similarly, the Apache web server is able to run the file as a CGI script, because it knows it must call the Perl interpreter.

Many scripting languages, such as Perl, use the # character to indicate the beginning of a comment line that should be ignored by the interpreter. The shebang is a special exception to this rule.

Using the correct shebang

The following table shows the correct shebangs to use in script files that run on A2 Hosting web servers:

Language Shebang
Perl #!/usr/bin/perl
Python (generic) #!/usr/bin/python
Python (specific version) #!/usr/bin/python2.7
#!/usr/bin/python3.6

... etc.
Ruby #!/usr/bin/ruby
PHP #!/usr/local/bin/php
You do not need to use the shebang for PHP script files that generate web output.

There is an alternative to using absolute paths to the interpreter in shebang lines. To make your script files more portable across different systems, you can use the env program to determine the path to the interpreter. For example, consider the following shebang line:

#!/usr/bin/env ruby

This example locates and runs the Ruby interpreter, whether it is located in the /usr/bin/ directory, the /usr/local/bin/ directory, or somewhere else. You do not have to know the absolute path to the Ruby interpreter, because the env program figures it out at runtime.

More Information

For more information about the shebang, please visit http://en.wikipedia.org/wiki/Shebang_(Unix).

Did you find this article helpful? Then you'll love our support. Experience the A2 Hosting difference today and get a pre-secured, pre-optimized website. Check out our web hosting plans today.

We use cookies to personalize the website for you and to analyze the use of our website. You consent to this by clicking on "I consent" or by continuing your use of this website. Further information about cookies can be found in our Privacy Policy.