我有这个(测试)脚本:
#!/bin/bash
my_cmd_bad_ ( ) {
cmd="$@"
$cmd
}
my_cmd_good_ ( ) {
"$@"
}
my_cmd_bad_ ls -l "file with space"
my_cmd_good_ ls -l "file with space"
输出是(文件不存在,这不是这个问题的重点):
» ~/test.sh
ls: cannot access file: No such file or directory
ls: cannot access with: No such file or directory
ls: cannot access space: No such file or directory
ls: cannot access file with space: No such file or directory
我很惊讶第一个版本没有按预期工作:参数没有被引用,而不是处理一个文件,而是处理三个。为什么?
如何保存要执行的命令并正确引用?我需要稍后执行它,我不再有它"$@"
了。
对此测试脚本的简单返工将不胜感激。