Perl | Use of Hash bang or Shebang line

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:

#!/usr/bin/perl

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.



filter_none

edit
close

play_arrow

link
brightness_4
code

use strict;
use warnings;
   
print "Hello World\n";

chevron_right


Now, we will save it with the name hello.pl and in the terminal, we will execute it by –

$ perl hello.pl

Output:
Hashbang in Perl
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:

$ hello.pl

Output:
Hashbang in Perl
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.

filter_none

edit
close

play_arrow

link
brightness_4
code

#!/usr/bin/perl
use strict;
use warnings;
   
print "Hello World\n";

chevron_right


If the above code is run as $ hello.pl, without executing the Perl first, then the output will be:
Hashbang in Perl
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.



My Personal Notes arrow_drop_up

Always a learner

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. 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.




Article Tags :
Practice Tags :


Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.