Perl | Use of Hash bang or Shebang line
Last Updated :
11 Jul, 2019
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.
Perl
use strict;
use warnings;
print "Hello World\n";
Now, we will save it with the name
hello.pl and in the terminal, we will execute it by -
$ perl hello.pl
Output:

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:

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.
Perl
#!/usr/bin/perl
use strict;
use warnings;
print "Hello World\n";
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.
Similar Reads
Non-linear Components In electrical circuits, Non-linear Components are electronic devices that need an external power source to operate actively. Non-Linear Components are those that are changed with respect to the voltage and current. Elements that do not follow ohm's law are called Non-linear Components. Non-linear Co
11 min read
Spring Boot Tutorial Spring Boot is a Java framework that makes it easier to create and run Java applications. It simplifies the configuration and setup process, allowing developers to focus more on writing code for their applications. This Spring Boot Tutorial is a comprehensive guide that covers both basic and advance
10 min read
Class Diagram | Unified Modeling Language (UML) A UML class diagram is a visual tool that represents the structure of a system by showing its classes, attributes, methods, and the relationships between them. It helps everyone involved in a projectâlike developers and designersâunderstand how the system is organized and how its components interact
12 min read
Backpropagation in Neural Network Back Propagation is also known as "Backward Propagation of Errors" is a method used to train neural network . Its goal is to reduce the difference between the modelâs predicted output and the actual output by adjusting the weights and biases in the network.It works iteratively to adjust weights and
9 min read
3-Phase Inverter An inverter is a fundamental electrical device designed primarily for the conversion of direct current into alternating current . This versatile device , also known as a variable frequency drive , plays a vital role in a wide range of applications , including variable frequency drives and high power
13 min read
Polymorphism in Java Polymorphism in Java is one of the core concepts in object-oriented programming (OOP) that allows objects to behave differently based on their specific class type. The word polymorphism means having many forms, and it comes from the Greek words poly (many) and morph (forms), this means one entity ca
7 min read
CTE in SQL In SQL, a Common Table Expression (CTE) is an essential tool for simplifying complex queries and making them more readable. By defining temporary result sets that can be referenced multiple times, a CTE in SQL allows developers to break down complicated logic into manageable parts. CTEs help with hi
6 min read
What is Vacuum Circuit Breaker? A vacuum circuit breaker is a type of breaker that utilizes a vacuum as the medium to extinguish electrical arcs. Within this circuit breaker, there is a vacuum interrupter that houses the stationary and mobile contacts in a permanently sealed enclosure. When the contacts are separated in a high vac
13 min read
Python Variables In Python, variables are used to store data that can be referenced and manipulated during program execution. A variable is essentially a name that is assigned to a value. Unlike many other programming languages, Python variables do not require explicit declaration of type. The type of the variable i
6 min read
Spring Boot Interview Questions and Answers Spring Boot is a Java-based framework used to develop stand-alone, production-ready applications with minimal configuration. Introduced by Pivotal in 2014, it simplifies the development of Spring applications by offering embedded servers, auto-configuration, and fast startup. Many top companies, inc
15+ min read