microlife
microlife

Reputation: 167

The command with double quotes in the shell cannot be executed

When I execute the following script, it will report an error, but if I manually execute the command of the echo, I will not report an error, they are clearly the same!

I tried to execute this script in Linux, Windows, they all report the same error. Although there is no command MinGW Makefiles on Linux, it will appreciate that even if there is, it will be reported. Because the error prompt is Could not create named generator "MinGW instead of Could not create named generator "MinGW Makefiles"

How can I fix this problem?

#!/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

Upvotes: 0

Views: 207

Answers (1)

chepner
chepner

Reputation: 532093

The point of using an array is to avoid cramming all the arguments into a single whitespace-delimited string.

#!/bin/bash

compiler_options=(-G "MinGW Makefiles")

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

Upvotes: 2

Related Questions