Connect with us

Bioinformatics Programming

How to execute Unix/shell commands in a Perl script?



BioPerl is a collection of Perl modules that are used to write Perl scripts applied in bioinformatics [1]. It is used in bioinformatics programmings such as in developing source codes, standalone software/tools, and algorithms. It’s easy to install and provide various modules which make it easier to execute different functions. However, Python is mostly preferred over the Perl language, still, some of the bioinformatics software is based on Perl such as the standalone version of I-TASSER. Sometimes, it’s a big trouble to execute some Unix/shell commands in Perl script for the beginners, it’s difficult to decide which function would be specific to a condition. Therefore, this article is a guide for the execution of Unix/shell commands in Perl script.

Perl offers different functions and operators to execute external commands (described as follows), which are special in their own ways. They are slightly different from each other, which sometimes makes it hard to choose for the beginners in Perl.

1. exec””

 syntax: exec "command";

It is a Perl function (perlfunc) which executes a command but never returns, as same as the return statement in a function. It keeps continuing processing the script while executing the command and doesn’t wait for it to finish first, it returns false when the command is not found but never returns true.

2. system()

syntax: system( "command" );

It is also a Perl function (perlfunc) that executes a command and waits for it to get finished first and then resume the Perl script. The return value is the exit status of the command. It can be called in two ways:

system( "command arg1 arg2" );


system( "command", "arg1", "arg2" );

3. Backticks “ or qx//

syntax: `command`;

syntax: qx/command/;

It is a Perl operator (perlop) very similar to system(), execute a command, and then resumes the Perl script after the execution has finished but the return value is STDOUT of the command.

4. IPC::Open2

syntax: $output = open2(\*CHLD_OUT, \*CHLD_IN, 'command arg1 arg2');

It runs a process for both reading and writing and creates a pipe to both STDIN and STDOUT. It can be used in both ways:

$output = open2(my $out, my $in, 'command arg1 arg2');

OR without using the shell

$output = open2(my $out, my $in, 'command', 'arg1', 'arg2');

You can read about it more in the documentation: IPC::Open2.

5. IPC::Open3

syntax: $output = open3(\*CHLD_IN, \*CHLD_OUT, \*CHLD_ERR'command arg1 arg2', 'optarg',...);

It is very similar to IPC::Open2 with the capability to capture all three file handles of the process, i.e., STDIN, STDOUT, and STDERR. It can also be used with or without the shell. You can read about it more in the documentation: IPC::Open3.

$output = open3(my $out, my $in, 'command arg1 arg2');

OR without using the shell

$output = open3(my $out, my $in, 'command', 'arg1', 'arg2');


syntax: a2p [options] [awkscript]

There is a Perl utility known as a2p which translates awk command to Perl. It takes awk script as input and generates a comparable Perl script as the output. Suppose, you want to execute the following awk statement

awk '{$1 = ""; $2 = ""; print}' f1.txt

This statement gives error sometimes even after escaping the variables (\$1, \$2) but by using a2p it can be easily converted to Perl script:

eval 'exec /usr/local/bin/perl -S $0 ${1+"[email protected]"}'
if $running_under_some_shell;
# this emulates #! processing on NIH machines.
# (remove #! line above if indigestible)

eval '$'.$1.'$2;' while $ARGV[0] =~ /^([A-Za-z_0-9]+=)(.*)/ && shift;
# process any FOO=bar switches
$, = ' '; # set output field separator
$\ = "\n"; # set output record separator

while (<>) {
chomp; # strip record separator
@Fld = split(' ', $_, -1);
if ($awk) {
$Fld[(1)-1] = '';
$Fld[(2)-1] = '';
print join($,,@Fld);
print join($,,@Fld) if $f1 . . $txt;

For further information, you can read it’s documentation: a2p


  1. Stajich, J. E.; Block, D.; Boulez, K.; Brenner, S.; Chervitz, S.; Dagdigian, C.; Fuellen, G.; Gilbert, J.; Korf, I.; Lapp, H.; Lehväslaiho, H.; Matsalla, C.; Mungall, C. J.; Osborne, B. I.; Pocock, M. R.; Schattner, P.; Senger, M.; Stein, L. D.; Stupka, E.; Wilkinson, M. D.; Birney, E. (2002). “The BioPerl Toolkit: Perl Modules for the Life Sciences”. Genome Research12(10): 1611–1618.

Tariq is founder of Bioinformatics Review and CEO at IQL Technologies. His areas of expertise include algorithm design, phylogenetics, MicroArray, Plant Systematics, and genome data analysis. If you have questions, reach out to him via his homepage.

1 Comment

1 Comment

  1. [email protected]

    November 10, 2021 at 2:24 am

    I have been using qq to run linux programs inside perl.
    And it works well.

    system qq($adduser –home $home –gecos “$fullname” $username);
    system qq($edquota -p john $username);
    system qq($chage -E \$(date -d +180days +%Y-%m-%d) $username);
    system qq($chage -l $username);
    system qq($quota -s $username);

You must be logged in to post a comment Login

Leave a Reply

Bioinformatics Programming Python script to search for binding affinities based on compound names.


on Python script to search for binding affinities based on compound names.

Previously, we have provided the script to analyze virtual screening (VS) results obtained from Autodock Vina. In this article, we have provided another script to search for binding affinity associated with a compound. (more…)

Continue Reading

Bioinformatics Programming

How to download files from an FTP server using Python?



How to download files from an FTP server using Python?

In this article, we provide a simple Python script to download files from an FTP server using Python. (more…)

Continue Reading

Bioinformatics Programming

How to convert the PDB file to PSF format?



How to convert the PDB file to PSF format?

VMD allows converting PDB to PSF format but sometimes it gives multiple errors. Therefore, in this article, we are going to convert PDB into PSF format using a different method. (more…)

Continue Reading