How to use the shebang

This article defines the shebang and how to utilize it to ensure that your script files execute properly.

What is the shebang?

In a script file, the shebang is a special character sequence that identifies which program should be called to run the script. The shebang, which consists of the characters #! followed by the path to the interpreter program, is always on the first line of the file. If necessary, you can also specify command line parameters.

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.

Using the correct shebang

The following table shows the correct shebangs to use in script files that run on Hostginger 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

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 perl

This example locates and runs the Perl 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 Perl interpreter, because the env program figures it out at runtime.

Was this answer helpful?

Related Articles

How to determine high disk usage locations

This article describes how to determine where your account is using the most disk space....

How to disable e-mail notifications from cron jobs

This article demonstrates how to stop receiving e-mail notifications from cron jobs. By default,...

How to manage the inode count

This article defines an inode count, discusses how to calculate it for your account, and how to...

How to monitor resource usage

This article describes how to monitor resource usage using the top and atop commands. Method 1:...

How to use cURL

This article describes how to use the cURL program to transfer files from the command line. When...