Chris
Chris

Reputation: 12181

execute perl in command line without specifying perl in UNIX

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

Upvotes: 5

Views: 37726

Answers (3)

user1126070
user1126070

Reputation: 5069

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.

Upvotes: 1

Shiplu Mokaddim
Shiplu Mokaddim

Reputation: 57650

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

Upvotes: 11

Kevin
Kevin

Reputation: 56089

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.

Upvotes: 2

Related Questions