I'm trying to store a command string in a variable for later execution. The problem is that the command includes a parameter argument with whitespace. When the command is executed, instead of seeing the argument as a single string, the shell interprets it as two, including the double-quote characters:
Example:
$ CMD_CMAKE="cmake -G \"MSYS Makefiles\" ../../source/poppler-0.79/"
$ ${CMD_CMAKE}
CMake Error: Could not create named generator "MSYS
I need to pass -G "MSYS Makefiles"
to the cmake
command. Instead, -G \"MSYS
and Makefiles\"
are being passed.
I have tried all of the following with no success:
$ CMD_CMAKE='cmake -G "MSYS Makefiles" ../../source/poppler-0.79/'
$ CMD_CMAKE="cmake -G 'MSYS Makefiles' ../../source/poppler-0.79/"
$ CMD_CMAKE="cmake -G MSYS\ Makefiles ../../source/poppler-0.79/"
$ CMD_CMAKE="cmake -G MSYS\ Makefiles ../../source/poppler-0.79/"
$ GEN="MSYS Makefiles" && CMD_CMAKE="cmake -G ${GEN} ../../source/poppler-0.79/"
$ GEN="MSYS Makefiles" && CMD_CMAKE="cmake -G "${GEN}" ../../source/poppler-0.79/"
$ CMD_CMAKE=$(echo "cmake -G "MSYS Makefiles" ../../source/poppler-0.79/")
$ CMD_CMAKE=$(echo 'cmake -G "MSYS Makefiles" ../../source/poppler-0.79/')
I have done some searching around on Google, but I am not finding any answers. Perhaps because I am not using the correct terminology.
Edit: I'm sorry, I think I should have posted this question on stackoverflow.com.
Should this question be deleted?
– AntumDeluge Aug 02 '19 at 21:37array=(cmake -G "MSYS Makefiles" ../../source/poppler-0.79/)
, you execute it with"${array[@]}"
(note the syntax and the quotes). – Kusalananda Aug 02 '19 at 22:05"${CMD_CMAKE[@]}"
worked. :) – AntumDeluge Aug 02 '19 at 22:13eval "${CMD_CMAKE}"
instead of${CMD_CMAKE}
alone instead. – frabjous Jun 23 '22 at 19:19