tags:

views:

37

answers:

2

I usually see

#!/usr/bin/php

or

#!/usr/bin/perl

at the top of a CGI file. How is a

#!/usr/bin/env php

or

#!/usr/bin/env perl

different from the first 2 lines? They work as well.

+2  A: 

This is documented on the Wikipedia page for env. It basically runs the command without modifying existing environment variables.

BrianLy
+3  A: 

Shebangs, the #!/usr/bin/sh part of the script, require an absolute path to the interpreter. Because there is no standard path for some interpreters some scripts use #!/usr/bin/env interpreter to launch the correct interpreter.

Mark Trapp
env as in bash or csh or sh? How does it decide?
動靜能量
It'll use whatever SHELL is set to for the user invoking /usr/bin/env, but I'm not sure how it's relevant: invoke the intrepreter you want with "/usr/bin/env interpreter". If you want to use bash, use "/usr/bin/env bash".
Mark Trapp
can't `bash` set PATH and `sh` set PATH that are totally different? And then php refers to php5 in `bash` and refers to php4 in `sh`
動靜能量
Ah, yes. It should use the environment variables for the shell specified in /etc/passwd, but I'm not exactly sure how it works for non-standard shells like /dev/null or /bin/false. If you invoke env manually, it'll use the environment variables for whatever shell you're currently in.
Mark Trapp