Search code examples
phpshellsyntax-errorshell-exec

Syntax Error in shell_exec but not in terminal


I am trying to run a php script and do a loop. This is the code I use in terminal and it works:

for i in {1..60}; do printf "file '%s'\n" video.mp4 >>list.txt; done

However, if I try to call it in my php script as shell_exec():

shell_exec('for i in {1..60}; do printf "file '%s'\n" video.mp4 >>list.txt; done');

I get error:

PHP Parse error: syntax error, unexpected ''\n" video.mp4 >>list.txt; don' (T_CONSTANT_ENCAPSED_STRING), expecting ',' or ')' in /home/vagrant/Code/play/index.php on line 58

Parse error: syntax error, unexpected ''\n" video.mp4 >>list.txt; don' (T_CONSTANT_ENCAPSED_STRING), expecting ',' or ')' in /home/vagrant/Code/play/index.php on line 58


Solution

  • You must escape single quotes.

    shell_exec('for i in {1..60}; do printf "file \'%s\'\n" video.mp4 >>list.txt; done');