这工作正常:
$ eval 'echo "1" > ~/Desktop/in/foo'
但不是这个:
$ while IFS= read _cmd; do eval "$_cmd"; done < <(cat << EOF
'echo "1" > ~/Desktop/in/foo'
'echo "2" > ~/Desktop/in/bar'
EOF
)
bash: echo "1" > ~/Desktop/in/foo: No such file or directory
bash: echo "2" > ~/Desktop/in/bar: No such file or directory
如何解决?
更新
正如建议的那样,删除单引号是可行的,但不适用于这种变体(同上,仅用单引号将文件名括起来):
$ while IFS= read _cmd; do eval "$_cmd"; done < <(cat << EOF
echo "1" > ~/Desktop/in/foo (bis)
echo "2" > ~/Desktop/in/bar
EOF
)
连同提供的解决方案:
"$SHELL" << EOF
echo "1" > ~/Desktop/in/'foo (bis)'
echo "2" > ~/Desktop/in/bar
EOF