我很好奇为什么命令:
for f in `/bin/ls /mydir | sort | tail -n 10`; do echo $f; done;
输出/mydir 中的最后十个文件,但
/bin/bash -c "for f in `/bin/ls /mydir | sort | tail -n 10`; do echo $f; done;"
输出“意外标记附近的语法错误'[/mydir 中的文件]'”
最佳答案
您使用的是双引号,因此父 shell 在将参数传递给 /bin/bash
之前插入反引号和变量。
因此,您的 /bin/bash
正在接收以下参数:
-c "for f in x
y
z
...
; do echo ; done;"
这是一个语法错误。
为避免这种情况,请使用单引号传递您的参数:
/bin/bash -c 'for f in `/bin/ls /mydir | sort | tail -n 10`; do echo $f; done;'
关于bash -/bin/bash -c 与直接执行命令有何不同?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/26167803/