0

This is a pretty simple one... I just want to make a perl script executable without the preceding perl command, and instead let the environment deduce the interpreter from the shebang line. Here is my sample script called test:

#!/usr/bin/perl
print "Hey there\n";

I then use chmod 775 test to make the script executable. If I use the command perl test, I get the output Hey there.

However, if I just type test, I get no output. What's the deal? Why isn't my shebang line making the environment realize this is perl? Can someone please help me?

RobEarl
  • 7,862
  • 6
  • 35
  • 50
jake9115
  • 3,964
  • 12
  • 49
  • 78
  • 2
    Try `./test` or put your script somewhere in the path. – mpapec Feb 07 '14 at 16:03
  • 3
    Try renaming your script. `test` is a built-in command in most shells. – Barmar Feb 07 '14 at 16:04
  • See SO question [Why do you need ./ (dot-slash) before script name to run it in bash?](http://stackoverflow.com/questions/6331075/why-do-you-need-dot-slash-before-script-name-to-run-it-in-bash) – ThisSuitIsBlackNot Feb 07 '14 at 16:05
  • Sorry guys, I forgot to mention the folder I was working in was already added to $PATH. And yes, the problem was that I named the file test! `Sample` worked. thanks! – jake9115 Feb 07 '14 at 16:07
  • So, my example script works, but the software I'm trying to get running doesn't. I get the error message: `bash: /usr/bin/SampleSoftware: /usr/bin/perl^M: bad interpreter: No such file or directory`. What is going on here? I thought I had a bad line ending after my `shebang` line, but I don't think that is the case – jake9115 Feb 07 '14 at 16:10
  • 2
    Run `dos2unix` on your script. – ThisSuitIsBlackNot Feb 07 '14 at 16:10
  • Also, you're now asking a completely different question, invalidating all the existing answers. Don't do that. – ThisSuitIsBlackNot Feb 07 '14 at 16:12

2 Answers2

6

Don't name your script test. This is a built-in command in most shells, so they don't go looking for an external program.

Also, to run a program in your current directory, you should type ./programname. It's generally a bad idea to have . in your $PATH, which would be necessary to execute it without the directory prefix.

Barmar
  • 741,623
  • 53
  • 500
  • 612
0

To run something from the current directory you need to prefix "./" to tell it "this directory" ie ./testprogram.

If you type just test it will look in standard install directories like /bin. This is why when you run cp or rm it knows where the executable is.

As mentioned by others, naming scripts test is not allowed with most shells.

Chris L
  • 2,262
  • 1
  • 18
  • 33
  • 4
    In the specific case of `test`, most shells won't look anywhere, because they implement it as a builtin, which takes precedence over external binaries. – cjm Feb 07 '14 at 18:08
  • Edited to remove reference to `test` – Chris L Feb 11 '14 at 09:40