这个问题的扩展: Bash:向带空格的字符串添加额外的单引号
将命令的参数存储为 bash 数组后
touch "some file.txt"
file="some file.txt"
# Create an array with two elements; the second element contains whitespace
args=( -name "$file" )
# Expand the array to two separate words; the second word contains whitespace.
find . "${args[@]}"
然后将整个命令存储在数组中
finder=( find . "${args[@]}" )
在 bash 中,我可以运行如下命令:
"${finder[@]}"
./some file.txt
但是当我尝试使用期望时,我得到了错误
expect -c "spawn \"${finder[@]}\""
missing "
while executing
"spawn ""
couldn't read file ".": illegal operation on a directory
为什么这里没有发生 bash 变量扩展?