MartyIX
MartyIX

Reputation: 28646

How to change the shell that is used with shell_exec in PHP?

I'm using Windows and I would like to set up PHP so as to use bash shell (installed on my machine thanks to msysgit). The reason is that I have Windows on my development machine and Linux on the production machine.

Thanks!

Upvotes: 0

Views: 653

Answers (1)

Janus Troelsen
Janus Troelsen

Reputation: 21298

You can't. But you can launch your own shell like c:\mingw\bin\sh -c 'set'. That would require Mingw on your machine of course.

Using Cygwin it would be:

shell_exec("C:\cygwin\bin\bash.exe --login  -c '/cygdrive/c/cygwin/bin/convert.exe --version'");

There is a limit on command line length, therefore I wouldn't recommend putting the script itself there if it is more than just a one-liner. The options just after the shell script interpreter's name, are for the interpreter. If you want to pass arguments to the script the interpreter executes, it needs to be inside the quotes, i.e. just after ".exe" in the convert example.

You can pipe a script to the shell script interpreter also. That way, you wouldn't need to write it to a file, and you could still use a long script.

I.e.:

$ echo "echo 'Hello world';" | sh
Hello world

See http://php.net/manual/en/function.proc-open.php for how to use pipes in PHP.

Upvotes: 4

Related Questions