Practical Extraction and Reporting Language or Perl is an interpreter based language. Hash-bangs or shebangs are useful when we are executing Perl scripts on Unix-like systems such as Linux and Mac OSX. A Hashbang line is the first line of a Perl program and is a path to the Perl binary. It allows invoking the Perl scripts directly, without passing the file to the Perl as an argument. A Hashbang line in Perl looks like:
A Hashbang line is called so because it starts with a Hash(#) and a bang(!). A hashbang line in Perl holds significant importance in a Perl code. Now, let’s get started with the use of this Hashbang line.
Example: Suppose we have a hello world program script of Perl which we will execute on a Linux system with the terminal.
Now, we will save it with the name hello.pl and in the terminal, we will execute it by –
$ perl hello.pl
Here, in the above code, the terminal is running the Perl first and then the Perl is asked to run the code script. If the code script is run without invoking the Perl first then an error arises.
Try running the code as shown below:
Here, the shell we used, tried to interpret the commands in the file. However, it could not find the command print in Linux/Unix. Hence, there is a need to inform shell that it is a Perl script. This is the point where the concept of Hashbang comes into action. Hashbang informs the terminal about the script.
However, before executing this code, there is a need to set the path of the shell to add the current directory to existing directories. This can be done by executing the following command:
$ PATH = $PATH:$(pwd)
This will append the current working directory to the list of directories in the PATH environment variable.
Next, there is a need to add the Hash-bang line #!/usr/bin/perl in the Perl script file hello.pl. This line is always added at the beginning of the code i.e. the first line of the code script is the Hashbang line.
If the above code is run as $ hello.pl, without executing the Perl first, then the output will be:
Above code works fine and no error is produced because of the Hashbang line #!/usr/bin/perl added as the first line of the script.
When the script is executed it is run in the current shell environment. If the script starts with a hash and a bang (hash-bang) #! then the shell will run execute the application that has its path on the hash-bang line (in this case /usr/bin/perl) which is the standard location of the Perl compiler-interpreter. So, the hash-bang line holds the path to the Perl compiler-interpreter.
Now, the error occurs when there is no hash-bang line in the file and we try to execute it without running Perl explicitly. The shell assumes that the script is written in Bash and thus tries to execute it accordingly leading to errors.
- Perl Hash
- Perl | Useful Hash functions
- Perl | Hash Operations
- Sorting Hash in Perl
- Perl | Hash in Scalar and List Context
- Perl | Line Action Commands in a Debugger
- Perl | Multi-line Strings | Here Document
- Perl | Basic Syntax of a Perl Program
- Perl Tutorial - Learn Perl With Examples
- Node | urlObject.hash API
- C# | How to get hash code for the specified key of a Hashtable
- How to read a hash with an “&” sign in the URL ?
- How to Secure hash and salt for PHP passwords ?
- hash command in Linux with examples
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.