Aug 23, 2022 3 min read

How to Run Perl scripts in Linux Command Line

Table of Contents

Call them ancient but Perl scripts are still a great way of scripting automation tasks for various sysadmin tasks.

If you are new to it and wondering how to run Perl scripts in the Linux terminal, just use this command:

perl script.pl

This will work even if the script file doesn't have the execute permission.

You may also run it like a bash script:

./script.pl

But for that to work, the file must have the execute permission and the script must begin with the hashbang:

#!/usr/bin/perl

As long as the syntax is right and Perl installed on your system, you should be able to run Perl scripts easily.

How do you know if Perl is installed on your system? Use this command and if you see the version details, you should be good.

perl --version
Check installed verison of Perl
Checking if Perl is installed

If your system throws an error like "Perl command not found," use your system's package manager to install it first.

There are other ways of using Perl in Linux. You can write a Perl script directly in the terminal and run it immediately, without even saving it.

Let's see things in a bit more detail.

Executing Perl scripts from the files

This is the recommended way to create and execute Perl scripts. You have the scripts saved and you can use them in an IDE like Eclipse if the script is complicated or part of a larger Perl project.

Perl file does not need to have any extension, but a ".pl" extension is good practice.

Create a new file using the touch command like this:

touch my_file.pl

The recently created file will be empty, I'll be using Nano editor to modify the empty file.

nano my_file.pl

My sample script looks like this:

#!/usr/bin/perl
print "Enter your name: ";
$name=<STDIN>;
print "Hello, ${name} ... you will soon be a Perl addict!";
print "\n";
Modifying Perl script in nano text editor
Using nano to edit Perl script

Save and exit from the Nano editor using the Ctrl+X key.

No need to give execute permission to the script. Use it like this:

perl my_file.pl
Execute Perl script from file
Executing Perl script from file

You can also give the script execute permission and run it like this:

./my_file.pl

That's easy, right? But if your aim is to just check a Perl syntax, you don't need to create and save scripts.

Execute a single line of Perl command in Linux

This is a quick method for quickly checking Perl syntaxes.

To run your Perl command, you just have to follow the given syntax:

perl -e <Perl code>

You just have to pay attention to the quotes in bash shell.

perl -e 'print "Hello world!\n";'
Execute Perl script with single quotes to avoid issues with bash
Executing Perl script with single quotes 

It works with a single line of Perl code. You can pass multiple lines with multiple -e options. However, there is a better way of writing multiple lines of Perl code without saving them in a file.

Executing Perl commands without saving them in a file

This method is a bit similar to what you saw above, but it is less complicated.

Start the Perl prompt with the given command:

perl

Then you write your desired script. Once you're done writing, you must press CTRL + D to indicate the prompt End of File (EOF). The Perl script will be executed immediately as you press Ctrl+D.

For instance, I want to run the following script:

print "Enter your name: ";
$name=<STDIN>;
print "Hello, ${name} ... you will soon be a Perl addict!";
print "\n";

This simple script is supposed to take input from the user (name in this case) and will return something like this:

Execute Perl script in Perl prompt
Running Perl script using Perl command and prompt

As you can see, running Perl scripts in Linux is not much different than running a bash shell script. Perl is a powerful scripting language and though Python has gained popularity, seasoned sysadmins still use it for complex scripting. Enjoy.

Sagar Sharma
A software engineer who loves to tinker with hardware till it gets crashed. While reviving my crashed system, you can find me reading literature, manga, or watering my plants.
Great! You’ve successfully signed up.
Welcome back! You've successfully signed in.
You've successfully subscribed to Linux Handbook.
Your link has expired.
Success! Check your email for magic link to sign-in.
Success! Your billing info has been updated.
Your billing was not updated.