Server Fault is a question and answer site for system and network administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I want to write something detecting where PHP is installed and then execute php after that eg:

./ /path/to/php/file.php

So I can get it to detect the php I want to use and stick it in a variable, but I don't know how to get it to execute the following php file after. Here's a skeleton of what I've got:

if [ -e /usr/local/zend/bin/php ]; then
$PHP <execute php>

Edit - thanks got it, just needed exec $PHP "$@"

share|improve this question
In your script, why not just set the PATH to locations you think php will be and then run php and let bash do the testing. – Zoredache Nov 4 '10 at 2:22
Can't do it like that - we have at least 30 servers out there, we're and they have php installed in a different place. this will stop us having to see where it's been installed and "just work" - it goes to their server over our svn up cron already – user59142 Nov 4 '10 at 2:45
@Zordache: Using conditionals gives you more control. PATH=foo:bar:$PATH What if you only want to execute it if it's in foo or bar but not in the previous PATH (exiting with an error), but you need the previous PATH to be intact for other commands? I would suggest, however, that Michael change his else to elif [ -e /usr/bin/php ]; then and add an else echo 'PHP not found'; exit 1 or similar. – Dennis Williamson Nov 4 '10 at 2:59
@Dennis Williamson yes I thought about getting error stuff in there, might do, it will echo out "-bash: /blah/php: No such file or directory" if it can't find it in either of those directories anyway won't it? – user59142 Nov 4 '10 at 3:43
up vote 2 down vote accepted

Make your last line:

exec $PHP "$@"

That will run the php binary with the arguments you gave to the script and have it take over the shell script process as well.

If you would like to have your script do other things after executing the php script, drop the exec.

share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.