On OS X and Linux I can run .php files from from the command line if two things are true:
- I set the executable bit (
chmod +x file.php) - I have added
#!/usr/bin/phpto the top of the file.
If I want to run a shell script, however, I noticed that I can omit step 2, and it will use a default interpreter, /usr/bin/sh
Is there a configuration options I can set, that basically says “if the file is executable (chmod +x) and the extension is .php then use /usr/bin/php as the interpreter?
My ideal solution would allow file.php to live in /localhost/www/, and allow it to be accessed via a web browser as http://localhost/file.php or in terminal via:
/localhost/www/$ ./file.php
If there is some way to associate a MIME type other than by looking at the interpreter line or the extension, you could do it. But I suspect what you’re really asking is “magically recognize random PHP scripts that don’t mark themselves as such”, and the “magically” part means the answer is “no”. Humans can recognize it with sufficient reliability, but your computer can’t without help. Which is why there are file extensions and interpreter lines. (In general it won’t be smart enough to reliably distinguish PHP from Perl.)
If you do have an extension (
.phpfrom your subject line, as opposed to the.shin the question itself), thenzshhas some ability to configure that butbashdoesn’t as yet. However, you can makeopenon OS X andxdg-openon Linux handle it correctly; the former is configured fro the Get Info dialog and the latter viaxdg-mimeor your desktop’s MIME type database (the latter preferred as sometimes getting raw XDG settings imported is a black art).