0

I'm trying to give two commands on one line to shell_exec however there is no output. However, if passing only one command at a time this works perfectly fine:

$output = shell_exec('whoami');
echo($output); // This works
$output = shell_exec('dir');
echo($output); // This works as well
$output = shell_exec('whoami; dir');
echo($output); // No output...

What am I missing? I'm running a XAMPP (3.2.4.) environment on Windows 10 Build 20H2.

1

3 Answers 3

0

you can use the && sign to join the commands

$output = shell_exec('whoami && dir');
echo($output);

this should work pretty fine

Sign up to request clarification or add additional context in comments.

Comments

0

I found the answer. The problem was that using the semicolon ";" between two commands only works on Linux Systems. Because I'm operating on Windows I had to use the "&" instead.

Comments

0

To add on our answer,You can create a workaround for os variance:

if (strtoupper(substr(PHP_OS, 0, 3)) === 'WIN') { $commandSeparator='&&'; } 
else { $commandSeparator=';'; }

then you can add it to the command.

$commandToExecute=$commandOne.$commandSeparator.$commandTwo;
$output=shell_exec($commandToExecute);
echo($output);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.