Current Location: Home> Latest Articles> Detailed Example of Writing Shell Scripts with PHP CLI | PHP Command Line Tutorial

Detailed Example of Writing Shell Scripts with PHP CLI | PHP Command Line Tutorial

gitbox 2025-07-29

Introduction

PHP is a popular backend development language. Besides enabling dynamic functionality on web pages, it can also execute scripts via the command line. This article will provide a detailed example of writing a simple Shell script using PHP in command line mode.

Environment Setup

Before getting started, ensure PHP is properly installed and the php command can be run from the command line. You can verify this by entering the following command in your terminal:

<span class="fun">php -v</span>

Creating a Shell Script

Creating the File

First, open your text editor and create a file named shell.php. Add the following code to the file:

<span class="fun"><?php<br />$cmd = $_POST['cmd'];<br />$result = shell_exec($cmd);<br />echo "Command: " . $cmd . "<br/>";<br />echo "Result: " . $result . "<br/>";<br />?></span>

In this script, we retrieve the user’s input command from the $_POST array, execute it using the shell_exec function, and assign the output to the $result variable. Finally, we display both the command and its result using echo.

Running the Script

After saving and closing shell.php, run the script from the command line using the following command:

<span class="fun">php shell.php</span>

You have now successfully created a simple Shell script that can be executed from the command line.

Testing the Shell Script

To test the Shell script, try executing some simple commands. In the command line, enter:

<span class="fun">php shell.php</span>

Then, input the following command:

<span class="fun">echo Hello World</span>

You should see output similar to:

<span class="fun">Command: echo Hello World<br />Result: Hello World</span>

This indicates that the Shell script is functioning correctly and can execute basic commands.

Enhancing Security

To improve the security of the script, you can validate user input. For example, only allow specific commands or filter and escape input.

Here is an example that only permits the cat command, rejecting all others:

<span class="fun"><?php<br />$cmd = $_POST['cmd'];<br />$allowedCommands = array('cat');<br />if (in_array($cmd, $allowedCommands)) {<br />    $result = shell_exec($cmd);<br />    echo "Command: " . $cmd . "<br/>";<br />    echo "Result: " . $result . "<br/>";<br />} else {<br />    echo "Invalid command!";<br />}

By limiting the commands users can execute, you reduce potential security risks.

Conclusion

In this article, you have learned how to write and run Shell scripts using PHP in command line mode. We demonstrated how to capture user input, execute commands, display results, and apply basic security measures to protect the script.

Using the command line mode allows you to perform automated tasks and batch operations more efficiently. We hope this guide helps you better understand and utilize PHP in command line mode.