Guide to Run Perl Scripts on the Linux Command Line

Share this:

Last updated on December 17th, 2022 at 02:43 pm

Call them ancient, but Perl scripts are still a great way to automate various sysadmin tasks. If you are a beginner and wondering how to run Perl scripts in a Linux terminal, just use this command:

How to Run Perl Scripts on the Linux Command Line
How to run Perl Scripts on Linux command Line

This will work even if the script file does not have permission to execute.


You can also run it as a bash script:


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


If the syntax is correct and Perl is installed on your system, you should be able to execute Perl scripts easily.

Read also: How to split a Gzip file on Linux

How do you know if Perl is installed on your system?

Use this command and if you see the version information, then everything is fine.

perl --version
perl script

If your system gives you an error like “Perl command not found”, use your system’s package manager to install it.

There are other ways to use Perl on Linux.

You can write a Perl script right in the terminal and run it right away without even saving it.

Let’s look at this in a little more detail.

Executing Perl Scripts from Files

This is the recommended way to create and execute Perl scripts.

See also  Self-driving cars got stuck in the slow lane

You save scripts and can use them in an IDE like Eclipse if the script is complex or is part of a large Perl project.

The Perl file doesn’t have to have any extension, but the “.pl” extension is good practice.

Create a new file with the touch command like this:


The newly created file will be empty, I will use the Nano editor to modify the empty file.


My example script looks like this:

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

Save and exit the Nano editor with Ctrl+X.

You don’t need to give the script permission to execute.

Use it like this:


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


But if your goal is simply to check Perl syntax, you don’t need to create and save scripts.

Running a Single Line Perl Command on Linux

This is a quick method for a quick Perl syntax check.

To execute a Perl command, you just need to follow the given syntax:

perl -e <Perl code>

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

perl -e 'print "Hello world!\n";'

You can pass multiple lines with multiple -e options.

However, there is a better way to write multiple lines of Perl code without saving them to a file.

Executing Perl commands without saving them to a file

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

Run a Perl prompt with the given command:


Then write the desired script.

See also  Book train tickets online: A step-by-step guide

Once you are done writing, you should press CTRL+D to show the End of File (EOF) prompt.

The Perl script will be executed immediately after pressing Ctrl+D.

For example, I want to run the following script:

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

This simple script should take input from the user (name in this case) and return something like this:

Simple Script


As you can see, running Perl scripts on Linux is not much different than running bash shell scripts.

Perl is a powerful scripting language, and while Python is gaining popularity, experienced sysadmins still use it for complex scripting.

Checkout also: 11 reasons Vivaldi is the best browser you’re not using

Share this:

Leave a Comment