user2342558
user2342558

Reputation: 6703

php shell_exec silently fails if it contains curly brackets for string substitution

This simple command works fine if pasted in the terminal but in php it doesn't work.

for f in a b c d e; do echo ${f/a/p}; done; 2>&1

Terminal

p
b
c
d
e

Php

$command = "for f in a b c d e; do echo \${f/a/p}; done;";
$command .= " 2>&1";
echo shell_exec($command);

outputs nothing.

$command = "for f in a b c d e; do echo \$\{f/a/p\}; done;";
$command .= " 2>&1";
echo shell_exec($command);

output:

${f/a/p}
${f/a/p}
${f/a/p}
${f/a/p}
${f/a/p}

What's the problem??

Thanks

Upvotes: 3

Views: 256

Answers (1)

CreeperMaxCZ
CreeperMaxCZ

Reputation: 135

as @jhnc mentioned, the problem is it uses shell instead of bash. You can execute bash with command argument like this:

$command = 'for f in a b c d e; do echo ${f/a/p}; done;';
$command .= " 2>&1";
echo shell_exec("/bin/bash -c '" . $command. "'");

Output:

p
b
c
d
e

Upvotes: 4

Related Questions