Run Bash Command from PHP

Run Bash Command from PHP

  1. Blog
  2. 15 November 2026
  3. 3 min read

Stefan Bogdanescu

Founder & Senior Architect

Why Your Bash Script Fails in PHP But ls Works

When you move from a terminal to a web request, your PHP process often inherits a completely different environment than your interactive shell session. This discrepancy is almost certainly why your script fails while commands like shell_exec("ls") succeed. It isn't necessarily a VPS limitation; it is a matter of how the operating system initializes processes when they are spawned without an active user context.

The core issue usually lies in missing environment variables, specifically $HOME. When you run ./script.sh var1 var2 directly in your terminal, your shell exports your home directory, login scripts, and various paths into the environment. The bash script likely relies on these variables to locate configuration files, libraries, or binaries. However, when PHP spawns a new process via shell_exec, it does so as a non-interactive user with a stripped-down environment.

If your script expects $HOME to exist or tries to read from ~/.bashrc, that path will be missing or incorrect in the web context. You can verify this immediately by adding an echo statement to your PHP code:

$output = shell_exec('echo "HOME=$HOME"');
echo $output; // Likely returns empty string or "/" instead of /home/username

If the output is empty, your script cannot find its dependencies. The solution involves explicitly setting these variables within the command string passed to PHP. Instead of calling the script directly, prefix it with an environment override:

$output = shell_exec('HOME=/home/your-username bash -c "./script.sh var1 var2"');
echo "<pre>$output</pre>";

Using bash -c ensures that the script runs in a fresh shell session that mimics your interactive terminal, sourcing the necessary environment files. This is often more robust than relying on exec or system, as shell_exec returns the output directly to PHP variables, whereas system outputs to stdout and exec modifies $GLOBALS.

Another critical consideration is file permissions. Web servers typically run under a specific user account (like www-data or apache) rather than your primary user. Even if you fix the environment variables, the web server process might not have read/execute permissions on /path/to/script.sh or its arguments. Ensure the script and its dependencies are owned by root or world-readable:

chmod 755 /path/to/script.sh
chown root:root /path/to/script.sh

Additionally, check if the script attempts to write to a specific directory that the web server cannot access. If your script creates files or logs data, ensure the target directories are writable by the web server user.

Finally, consider the limitations of shell_exec. Capturing output from scripts that generate large amounts of text can cause memory issues in PHP, as the entire output is loaded into a string variable. For long-running tasks or those generating massive logs, redirecting the output to a file and having your script report completion via HTTP status or database flags is often safer than trying to parse the full output in PHP.

By aligning the environment variables and verifying permissions for the web server context, you can successfully execute complex bash logic from within PHP without relying on fragile assumptions about the runtime environment.

Our network