This question already has an answer here:

I have a file "commands.php" that dynamically generates bash commands. For example, one line is:

# - commands.php
ssh me@ echo "text" > file; reboot;

How can I use the output of xargs to execute these commands... eval, maybe?

cat commands.php | xargs -L 1 -I {} 'eval "$({})"';

marked as duplicate by Joseph R., jasonwryan, Anthon, Bernhard, slm Dec 25 '13 at 15:54

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

  • Sounds like XY problem. Why would you want to do this? What's wrong with bash /path/to/file? – Patrick Dec 24 '13 at 23:54
  • Hi Patrick, very well could be! I have a file that dynamically generates bash commands, and it is not a .sh file. (I could copy the output to an sh file, but am interested in how to execute each line of a non-sh file – d-_-b Dec 24 '13 at 23:56
  • Everything else aside, is this php script actually. Allen from the web? This seems very risky. – mattdm Dec 25 '13 at 0:47
  • @mattdm haha good catch! but yes this is a very secure environment, not some public external file. – d-_-b Dec 25 '13 at 0:55

Figured out a way:

cat commands.php | xargs -L 1 -I {} sh -c "{}"

runs each command out of shell... simple enough!

  • Why do you insist on cating the file? A simple redirection would be enough: xargs ... <commands.php – Joseph R. Dec 25 '13 at 2:12
  • And if you want to do it in parallel, use GNU Parallel: cat commands.php | parallel – Ole Tange Dec 27 '13 at 20:20

If your PHP file outputs shell commands, you could probably get away with

$( php /path/to/your/script.php )


while read line; do
done < $( php /path/to/your/script.php )

Not the answer you're looking for? Browse other questions tagged or ask your own question.