Using the shebang
Using the shebang
Learn what the shebang is and how to use it to ensure your script files run correctly.
This article explains the purpose of the shebang and how to use it to make scripts execute properly.
What is the shebang?
The shebang is a special character sequence at the beginning of a script file that tells the system which program should execute the script. It always appears on the first line and consists of #! followed by the path to the interpreter. You can also include command-line options if needed.
For example, the following line specifies the Perl interpreter and the -w option:
#!/usr/bin/perl -wThe shebang is especially important for CGI scripts. For instance, a file named script.cgi with this shebang can be executed from the command line like this:
./script.cgiThe Apache web server can also run the file as a CGI script because it knows to call the Perl interpreter.
Using the correct shebang
Here are recommended shebangs for scripts running on ruachost.com servers:
| Language | Shebang |
|---|---|
| Perl | #!/usr/bin/perl |
| Python (generic) | #!/usr/bin/python |
| Python (specific version) | #!/usr/bin/python2.7 #!/usr/bin/python3.6 … |
| Ruby | #!/usr/bin/ruby |
| PHP | #!/usr/local/bin/php |
You can also make scripts more portable by using env to locate the interpreter dynamically:
#!/usr/bin/env rubyThis finds the Ruby interpreter whether it is in /usr/bin/, /usr/local/bin/, or elsewhere, so you don’t need to know the absolute path.
More information
For further details about the shebang, visit Wikipedia: Shebang (Unix).