如何 运行 shell 命令与子 shell 和 PHP 的 exec?

How to run a shell command with a sub shell with PHP's exec?

运行 这个带有 PHP 的 exec 的命令给我语法错误,无论我是直接 运行 还是把它放在一个额外的文件中 运行那。

time (convert -layers merge input1.png $(for i in directory/*; do echo -ne $i" "; done) output.png)

我认为问题在于它创建了 exec 似乎无法处理的子 shell。

Syntax error: word unexpected (expecting ")")

尝试简化命令:删除不需要的外部 ()

您可以将 $(for i in directory/*; do echo -ne $i" "; done) 替换为 只是 directory/* 或者如果你是 担心空目录 $(shopt -s nullglob; echo directory/*)

time convert -layers merge input1.png directory/* output.png

time convert -layers merge input1.png $(shopt -s nullglob; echo  directory/*) output.png