I have a simple Hello World perl script.


print 'Hello world.';

It works fine if I run it via perl <file_name>, but it fails I run it via sh <file_name>.

My understanding of the first line is that it invokes the perl shell (similar to #!/usr/bin/bash will invoke a bash shell). So what exeactly is the difference between the two commands?

I found this thread http://ubuntuforums.org/showthread.php?t=1133334 that states sh <file_name> "In that case, you don't run Perl at all, but the shell, which will call the program 'print'." IF that's the case is the #! just being interpreted as a comment. If so, is it needed at all?

(I am aware that I can chmod +x the file and run it dirrectly, but I just want to know the difference between the two methods I was using)

3 Answers 3


If you execute a file directly


The shebang line will be searched for the interpreter to run it. If you run perl or sh with an argument, they'll behave as documented: try to interpret the file as a script in Perl or shell, respectively.

When you explicitly set the interpreter from the command line (such as sh foo.pl or perl foo.pl), the shebang line is not used to determine the interpreter to run. It is parsed for possible options, (for example with a #!/usr/bin/perl -w shebang, running the script as perl foo.pl will enable the -w flag) but it is not used to determine which program should interpret the script.

So, running a perl script as sh foo.pl means your system will try to interpret it as an sh script instead, despite the perl shebang line.

  • @terdon: Not completely correct with your update. perl will parse the shebang line in a script for switches.
    – Dubu
    Commented May 2, 2014 at 11:58
  • @Dubu even when that script is launched as sh foo.pl? Perl won't even be called in that case, how will it parse the shebang? The shebang is only read if you don't specify an interpreter (sh or perl or whatever) when launching the script. That's the problem that the OP is facing. It will indeed be parsed if you launch with the correct interpreter (perl foo.pl) but not if you use the wrong one (sh foo.pl).
    – terdon
    Commented May 2, 2014 at 12:02
  • @terdon: The Answer now says that the shebang line will be ignored when the script is called as an argument to perl or sh. I wanted to point out that this does not hold when called as perl <filename>.
    – Dubu
    Commented May 2, 2014 at 12:18
  • @Dubu fair enough. Clearer now?
    – terdon
    Commented May 2, 2014 at 12:24
  • @terdon Almost perfect. Whether the shebang line is interpreted depends on the interpreter, of course. perl does it, for sh I don't know.
    – Dubu
    Commented May 2, 2014 at 12:52

So you haven't made the script executable? sh isn't going to automatically assume that things with those file extensions are executable, so you either need to run perl <file name> or you need to make it executable with chmod +x


The two may not be the same depending on how Perl is installed on the system you're using. Try entering this command:

which perl

It will show you the path to the executable file called "perl" in your $PATH (type echo $PATH to see your $PATH). On many systems, this will be /usr/bin/perl, which is where Perl is usually installed - this is why the "shebang" line at the start of most Perl scripts also assumes it is there.

On some systems, though, the path to perl may be different. In that case, you may get a different result typing perl your_script.pl versus using the shell to interpret the shebang line, e.g. ./your_script.pl; in the former case, the perl interpreter in your $PATH will interpret your script, while in the latter case, the one at /usr/bin/perl will. These may not be the same.

In many cases, perl is your own installation of Perl, probably newer than the one that came with the system (in /usr/bin). In other words, there is more than one Perl interpreter on the system. In that case, you had better figure out which one you want and hardcode its path in your shebang line so that your script is not accidentally interpreted with the wrong version of Perl. (You can also write things like use 5.12; in your script for additional safety in this regard.) To check the version of the Perl interpreter, enter /path/to/perl -v.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .