views:

19

answers:

2

In this superuser question I was advised that it is better to execute scripts written in an interpretted language (php, python, etc) by explicitly executing the interpretter and providing the script as an argument, like:

> php script.php

rather than adding a line to the script to tell the OS to execute it, like:

#!/usr/bin/php
<?php
echo "hello world";
?>

Why is this true? My intuition tells me that it's safer, in case the script is moved to a system in which the interpreter's executable is located at a different path, but is that the only reason?

A: 

Different paths would be the primary reason, especially when binaries start getting stored in x64-denoted paths or installed in /usr/local/bin/php.

Ian Wetherbee
+1  A: 

Portability is enhanced if you use this idiom:

#!/usr/bin/env php

but it has drawbacks of its own; see a longer discussion at http://sites.google.com/site/frankpzh/knowledge-library/shebang

lhf