test.php:

<?php
print_r($argv);
?>

Shell:

$ php -q test.php foo bar
Array
(
    [0] => test.php
    [1] => foo
    [2] => bar
)
Answer from schneck on Stack Overflow
🌐
PHP
php.net › manual › en › function.shell-exec.php
PHP: shell_exec - Manual
SUID scripts open up security holes, so you don't always want to go this route even if it is an option. Write a simple binary and elevate the privileges of the binary as a SUID. In my own opinion it is a horrible idea to pass a system command through a SUID-- ie have the SUID accept the name of a command as a parameter. You may as well run Apache as root! ... I'm not sure what shell you are going to get with this function, but you can find out like this: <?php $cmd = 'set'; echo "<pre>".shell_exec($cmd)."</pre>"; ?> On my FreeBSD 6.1 box I get this: USER=root LD_LIBRARY_PATH=/usr/local/lib/apache2: HOME=/root PS1='$ ' OPTIND=1 PS2='> ' LOGNAME=root PPID=88057 PATH=/etc:/bin:/sbin:/usr/bin:/usr/sbin SHELL=/bin/sh IFS=' ' Very interesting.
🌐
GitHub
github.com › mikehaertl › php-shellcommand
GitHub - mikehaertl/php-shellcommand: A simple object oriented interface to execute shell commands in PHP · GitHub
$command = new Command('jq'); $command->setStdIn($fh); if (!$command->execute()) { echo $command->getError(); } else { echo $command->getOutput(); } fclose($fh); <?php // Create command with options array $command = new Command([ 'command' => '/usr/local/bin/mycommand', // Will be passed as environment variables to the command 'procEnv' => [ 'DEMOVAR' => 'demovalue' ], // Will be passed as options to proc_open() 'procOptions' => [ 'bypass_shell' => true, ], ]);
Starred by 324 users
Forked by 55 users
Languages   PHP
Top answer
1 of 3
5

The problem here is that you are quoting the entiore command you are trying to run as a single variable. As a result, you're not running php with foo.php as an argument but instead are attempting to execute a file called php foo.php. Here's a simpler example to show you what I mean:

$ var1="echo "
$ var2="foo"
$ set -x ## debugging info
var1$var2"
+ 'echo foo'      ### The shell tries to execute both vars as a single command
bash: echo foo: command not found

var1" "$var2"
+ 'echo ' foo     ### The shell tries to execute 'echo ' (echo with a space)
bash: echo foo: command not found  

So, the right way is to remove the space and quote each variable separately:

$ var1="echo"
$ var2="foo"
var1" "$var2"

If you do that though, you'll hit the next error. The . is the source command. That tries to read a shell script and execute it in the current session. You are giving it a php script instead. That won't work, you need to execute it, not source it.

Finally, always avoid using CAPITAL variable names. The shell's reserved variables are capitalized so it's a good idea to always use lower case variable names for your scripts.

Putting all this together (with a few other minor improvements), what you want is something like:

#!/bin/sh

list="/path/to/my/site/dir"
config="/usr/bin/php"

for i in "$list"
do
    "$config" "$i"/test.php
done
2 of 3
5

The problem in your code is the line:

. "${CONFIG}${i}/test.php"  

Remove the .


Here is another example:

$ ls -l

-rwxrwxr-x 1 bg bg 67 Oct 20 09:42 index.php
-rwxrwxr-x 1 bg bg 68 Oct 20 09:43 test.sh

index.php

<?php
    shell_exec('echo Hello > /tmp/hello.txt');
?>

test.sh

#!/bin/bash
/usr/bin/php index.php
🌐
Linux Hint
linuxhint.com › execute_shell_command_php
Execute Shell Command in PHP using exec() – Linux Hint
Create a PHP file with the following script. Two optional arguments of exec() are used in this script. ‘ls -l‘ command is used in the first argument that returns the list of directories. $output variable is used here to store the output of the command in an array. $status variable is used to store the return status value of the executed command.
🌐
GeeksforGeeks
geeksforgeeks.org › php › php-shell_exec-vs-exec-function
PHP shell_exec() vs exec() Function - GeeksforGeeks
July 11, 2025 - It allows running shell commands ... automating system tasks. ... Parameters: This function accepts a single parameter $cmd which is used to hold the command that will be executed....
Find elsewhere
🌐
Piotr Horzycki
peterdev.pl › execute-a-shell-command-in-php
Executing shell commands from a PHP script | Piotr Horzycki - Java and PHP developer’s blog
April 2, 2021 - passthru() executes a command and passes the raw output directly to the browser. The PHP documentation recommends it in case if binary output has to be sent without interference. shell_exec() executes a command and returns the complete output as a string. It does not provide the exit code.
🌐
Linux Tip
linuxscrew.com › home › programming › php › how to execute php from the command line (bash/shell)
How to Execute PHP from the Command Line (Bash/Shell)
October 22, 2021 - Multiple lines of code can be passed separated by a semicolon, or a heredoc (multiline Bash variable) can be piped in. Parameters/Arguments can be passed to PHP using the $argv variable, which is available when PHP is executed from the command line.
🌐
DaniWeb
daniweb.com › programming › web-development › threads › 19707 › invoking-a-shell-script
php - Invoking a shell script | DaniWeb
The difference between exec, system and shell_exec is the way the output is treated (under Windows at least with PHP < 4.3.x these functions behave strange depending on the machine). You can also use the backtick ooperator: http://cr.php.net/manual/en/language.operators.execution.php · <?php $output = `ls -al`; echo "<pre>$output</pre>"; ?> my shell script is running from PHP from web browser partially ,means it is creating a temprory file but not executing the gblinkapp,well this run through console
🌐
Ubuntu Forums
ubuntuforums.org › showthread.php
[all variants] [SOLVED] Passing PHP Variables to a shell script
September 6, 2008 - cat mailbox_creation.pl #!/usr/bin/expect ##set the get the username argument from create_email_address.php set username [lindex $argv 0] ##Run the Expect script spawn telnet localhost 25 #expect "2?: *" { send "helo domain.tld\n" expect "250 mail1.domain.tld" send "mail from: root@localhost\n" expect "2?? " send "rcpt to:<$username>\n" expect "2?? " send "data\n" expect "354 End data with <CR><LF>.<CR><LF>" send "Created User Mailbox\n" expect "\n" send ".\n" expect "2??
🌐
Anto
anto.online › home › code › how to execute shell commands via php
How to execute shell commands via PHP - Anto ./online
June 17, 2022 - It is easy to execute shell commands in PHP. Also, the shell_exec(), exec() or system() function must not be disabled in the php.ini file. How to view shell_exec() errors in PHP · Test your PHP code as the www-data user · How to submit an associative array with HTML & PHP · How to use functions in Bash Scripting ·
🌐
Scaler
scaler.com › home › topics › php exec() function
PHP exec() Function - Scaler Topics
March 18, 2024 - It allows you to pass command arguments and options as separate parameters. shell_exec(): The shell_exec() function executes commands within the context of a shell, which provides more flexibility and allows the use of shell features, such as ...