RSS Feed
Download our iPhone app
Browse DevX
Sign up for e-mail newsletters from DevX


Integrating Bash with PHP

Bash scripts can simplify many OS-oriented tasks for PHP developers. Even better, you can run Bash scripts directly from PHP, passing arguments, arrays, and receiving output that you can analyze or display.

his article discusses how to install and configure three Bash distributions for Windows: the GNU BASH shell for Windows, the Win-Bash Windows port, and the Cygwin tool. It also demonstrates how to execute Bash scripts using PHP, how to send different types of arguments to a Bash script, how to use Bash functions from PHP, and how to save results to a text file. (For information about other Bash distributions, see the sidebar Bash Availability.)

Download and Configure GNU Bash Shell for Windows

First, download the GNU Bash shell for Windows. The tool is ready to use immediately after you extract the ZIP archive in your favorite location (for example, C:\bash-2.03\). To simplify calling Bash, you can add the bash.exe path to your CLASSPATH setting.

Here's a standard HelloWorld Bash script:

   echo "Hello World!"
Name this script test.txt (in Windows it's most convenient to save Bash scripts as simple .txt files) and then open an MS-DOS command prompt. Navigate to the Bash home folder and type:

   >bash ${PATH_TO_SCRIPT}/test.txt
Author's Note: Replace the PATH_TO_SCRIPT with the complete path to hello.txt):

If you get the "Hello World" message then you have successfully executed your first Bash script. Now you can go further and add PHP into the equation.

Call Bash Scripts from PHP Using the shell_exec Function

To execute a Bash script using PHP use the shell_exec function, which has the following prototype:

   string shell_exec ( string $cmd ):  Execute a command through 
      the shell and return the complete output as a string. 
      The $cmd parameter represents the command which will be executed.
Author's Note: The shell_exec function is disabled when PHP is running in safe mode.

Here's a more complete example. This PHP application calls the HelloWorld Bash script created earlier:

$result=shell_exec("C:\bash-2.03\bash.exe test.txt");

When you run this, the output is:

   Hello World!
Here's a slightly more complex script that passes two integer values to a Bash script, which uses them to do some simple calculations, and then returns the results:

$result=shell_exec("C:\bash-2.03\bash.exe values.txt 2 4");
The preceding script sends two sample values, 2 and 4, to the Bash script values.txt, which contains the following code:

   echo 'sum = '$((a+b))    
   echo 'product = '$((a*b))
   echo 'average = '$(((a+b)/2))
The output is:

   sum = 6 product = 8 average = 3
You can send as many arguments as you like to a Bash script. Bash extracts them using positional arguments such as $1, $2…, $n. In the preceding example, when the script runs, $1 = 2 and $2 = 4.

PHP, HTML, and Bash

Bash scripts may be as complex as you like. For example, the next application generates a simple HTML document using a Bash script called from PHP. Here's the PHP script, called bashPHP.php:

   C:\bash-2.03\bash.exe bashHTML.txt");

And here's the Bash script that generates the HTML (bashHTML.txt):


# our html code
echo "<html>"
echo "<head><title>BASH-HTML</title></head>"
echo "<body>"
echo "<h4>This page comes from Bash</h4>"
echo "Hello Everybody!"
echo "</body>"
echo "</html>"
The output that appears in the browser is:

   This page comes from Bash
   Hello Everybody!
Author's Note: You can download all the sample code for this article.

Close Icon
Thanks for your registration, follow us on our social networks to keep up-to-date