Q:

A strange, but probably simple, shell_exec issue

If I run this command on the command line (on my Mac OS X):
echo -n "hello" > foo-cmd.txt

I get the expected result, namely a file foo-cmd.txt containing "hello" without any newline at the end.
However, if I run this PHP code:
<?php
shell_exec("echo -n \"hello\" > foo-php.txt");
?>

I get a file foo-php.txt containing the text "-n hello" followed by a newline! In other words, the argument -n sneaks in as output, instead of being treated as an argument!
How can I resolve this issue?

A:

Your command is using the shell's built-in version of echo which doesn't support the -n option.
Try /bin/echo instead.

