Reputation: 6703
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
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