shell中带双引号的命令无法执行

The command with double quotes in the shell cannot be executed

我执行下面脚本的时候会报错,但是我手动执行echo的命令就不会报错,明明是一样的!

我尝试在Linux、Windows中执行这个脚本,它们都报同样的错误。虽然在Linux上没有命令MinGW Makefiles,但即使有,也会被举报。因为错误提示是Could not create named generator "MinGW而不是Could not create named generator "MinGW Makefiles"

我该如何解决这个问题?

#!/bin/bash

compiler_options=(' -G "MinGW Makefiles"')

echo "cmake -S . -B build ${compiler_options}"
cmake -S . -B build ${compiler_options}
# run command
bash test.sh
# errorinfo
CMake Error: Could not create named generator "MinGW

使用数组的目的是避免将所有参数塞入一个whitespace-delimited字符串。

#!/bin/bash

compiler_options=(-G "MinGW Makefiles")

echo "cmake -S . -B build ${compiler_options[*]}"
cmake -S . -B build "${compiler_options[@]}"