Knowledge Base

Using the shebang in script files

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 #!/usr/bin/python
Python 2.6 #!/usr/bin/python2.6
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_%28Unix%29.