Command-Line – Is There a Command for Running a Script According to Its Shebang Line?

command lineexecutablescriptsshebang

If I want to execute a bash script which doesn't have its execution permission set, I can do:

bash script.sh

What should I use instead of bash if the script isn't executable and I don't know the correct interpreter? Is there a command that looks up the interpreter from shebang line and executes the script with it?

Best Answer

Yep. It is called perl. Some examples, with the corresponding interpreter in the shebang line of the file (the actual file extension doesn't matter):

perl foo.bash    # works
perl foo.lua     # works
perl foo.clisp   # works
perl foo.csh     # works
perl foo.php     # works
perl foo.gnuplot # works (no arguments)
perl foo.pl      # works (obviously)
perl foo.py      # works
perl foo.sh      # works
perl foo.tcl     # works
perl foo.rb      # works
perl foo.nodejs  # works
perl foo.r       # works
perl foo.oct     # works
perl foo.csharp  # works (no arguments)

This is mentioned in Perl's documentation:

If the #! line does not contain the word "perl" nor the word "indir", the program named after the #! is executed instead of the Perl interpreter. This is slightly bizarre, but it helps people on machines that don't do #! , because they can tell a program that their SHELL is /usr/bin/perl, and Perl will then dispatch the program to the correct interpreter for them.

Related Question