Why shell_exec Appears to Ignore PHP Variables Without Quotes
When invoking a shell script from PHP via shell_exec(), the behavior of variable expansion often trips up developers who assume it functions exactly like a standard function call in their application code. Consider a scenario where you define two variables, $my_url and $my_refer, intending to pass them as arguments to a script located at /tmp/my_script.php.
If your initial attempt looks like this:
$my_url = "http://www.somesite.com/";
$my_refer = "http://www.somesite.com/";
$page = shell_exec('/tmp/my_script.php ' . $my_url . ' ' . $my_refer');
And your script's argv shows only one argumentâthe path to the script itselfâyou are encountering a fundamental misunderstanding of how the underlying shell parses command lines. The issue isn't with PHP failing to pass the variable; it is with how the shell interprets the string before executing the command.
The Role of Shell Expansion
In Unix-like environments, the shell (typically bash or zsh) performs word splitting and pathname expansion based on specific characters: spaces, tabs, newlines, and special characters like $, backticks, and quotes.
When you concatenate strings in PHP without delimiting them properly for the shell, the shell sees the resulting string as a single continuous block of text unless it encounters whitespace or quote boundaries to split them into distinct arguments. However, there is a more critical issue at play here: variable interpolation.
If your PHP code outputs $my_url directly into the command string without quotes around the value in the final constructed string, and if that value itself contains characters that the shell deems special (like $, backticks, or quotes), the shell might misinterpret parts of the argument. More commonly, if you simply concatenate strings with spaces between them, you should theoretically get arguments. The confusion usually arises because the variables are being passed to shell_exec(), which returns the output of the command as a string in PHP, not an array.
However, the specific behavior described in your scenarioâwhere quoting fixes the argument count but empty strings appear otherwiseâsuggests that the values of $my_url and $my_refer might be empty or contain characters that break the shell's parsing logic if not handled correctly.
Why Quoting Works
When you modify the call to:
$page = shell_exec('/tmp/my_script.php "' . $my_url . '" "' . $my_refer . '"');
You are explicitly telling the shell that these values are single arguments, regardless of what characters they contain. The double quotes prevent word splitting and preserve the content as a single unit. This is why argv[1] and argv[2] populate correctly.
The alternative attempt using curly braces:
$page = shell_exec('/tmp/my_script.php {$my_url} {$my_refer}');
is syntactically incorrect for PHP string concatenation. Curly braces {} do not concatenate strings in PHP; they are used for array access or brace expansion within shells, but not for joining variables in a PHP string literal context unless the variable contains curly braces, which is rare. This likely resulted in the shell seeing the literal characters { and } as part of the argument or failing to split them correctly depending on the shell's version and configuration.
The Real Solution: Use an Array
While quoting your variables works, it introduces a significant maintenance burden. If $my_url changes to include spaces or special characters, you must remember to re-quote it. Furthermore, relying on string concatenation for command execution is a security risk if the input comes from user data, as it opens the door to shell injection vulnerabilities.
The robust, secure way to pass arguments to shell_exec() (or any shell command) is to use an array. The PHP engine handles the quoting and argument separation for you, ensuring that spaces, quotes, and dollar signs in your variables are treated literally and safely:
$my_url = "http://www.somesite.com/";
$my_refer = "http://www.somesite.com/";
$args = [
'/tmp/my_script.php',
$my_url,
$my_refer
];
$page = shell_exec(implode(' ', $args)); // Or better yet, use escapeshellarg in a loop or pass directly if supported by your specific wrapper
Actually, the most direct and secure method is simply:
$args[] = '/tmp/my_script.php';
$args[] = escapeshellarg($my_url);
$args[] = escapeshellarg($my_refer);
$page = shell_exec(implode(' ', $args));
Using escapeshellarg() ensures that even if $my_url contains a quote character or a newline, the shell will parse it as a single, safe argument without needing you to manually wrap it in quotes every time. This approach eliminates the guesswork of whether your variables need quoting and prevents subtle bugs caused by special characters in your data.
Final Note on shell_exec()
Remember that shell_exec() returns the output of the command as a string. If your script echoes arguments or returns data, ensure you are capturing it correctly. For more complex interactions, consider using proc_open which provides real-time access to stdout and stderr streams without spawning a full shell process, though for simple argument passing, shell_exec with escapeshellarg is the standard practice.