execute perl in command line without specifying perl in UNIX

34,862

Solution 1

You can do it this way,

  1. Find the interpreter/executors path. In this case its /usr/bin/perl or /usr/bin/env perl
  2. Add it to the first line of the file as #!/usr/bin/perl.
  3. Give execute permission to the file chmod +x example.pl

Now it will run

    $ ./example.pl

Solution 2

You need to make the top line (the "shebang") #!/usr/bin/perl (note the slash where you have a space). Then, first you need to make sure that is actually the correct path to your perl executable (type which perl to see where it is). If it's elsewhere, correct the path appropriately. Then you need to make sure the script has the execute permission set. Type ls -l example.pl, and look for an x in the first column (fourth character, in particular). If it's not there, you need to make the script executable with chmod a+x example.pl. Finally, to run the program, you need to use ./example.pl. The ./ tells your shell that you want the script in your current directory.

Solution 3

I have two thoughts to add:

1) To use example.pl test1.txt your path should contain a dot. echo $PATH /usr/bin:/usr/X11R6/bin:/usr/bin/X11:.

2) Your file end of line should be unix, \n. At least your shebang line contain excatly your perl path, ended with a \n.

Share:
34,862
Chris
Author by

Chris

product and software at curative, scaling COVID-19 testing nationwide prior: senior forward deployed software engineer — Elizabeth Warren for President at Reach head of batch, XX CEO, seneca systems principal software engineer, zenpayroll (now Gusto) software engineer, LivingSocial

Updated on July 09, 2022

Comments

  • Chris
    Chris almost 2 years

    There is a similar question about Windows, but it won't work for Unix based computers (OS X specifically).

    I want to type the name of a file, say example.pl or example.pl parametertext.txt, and have it know to execute perl.

    I specified #!/usr/bin/perl in the file so it can find the executable. Instead, I get the message:

    bash: example.pl: command not found