livy@linux:~$ qemu_options="-enable-kvm -name \"Virtual Machine 1\"";
livy@linux:~$ qemu_options="$qemu_options -cpu host -smp cores=4 -m 2G";
livy@linux:~$ echo "$qemu_options";
-enable-kvm -name "Virtual Machine 1" -cpu host -smp cores=4 -m 2G
livy@linux:~$ qemu-system-x86_64 $qemu_options;
qemu-system-x86_64: Machine: Could not open 'Machine': No such file or directory
I already tried and successfully preserved the quotes around Virtual Machine 1 string. Why does it not work? And how to quote it properly?
Why does it not work?
Because word splitting is performed after quotes are recognized on unquoted results of other expansions. The content of expansions is not scanned, only splitted.
how to quote it properly?
Use bash arrays.
If not, use a function.
If you have exhausted any other alternatives, enter the dark side with eval
and properly double-quote arguments and re-evaluate them upon call. Remember that eval
is one letter away from evil.
qemu_options="$(printf " %q" -enable-kvm -name "Virtual Machine 1")"
qemu_options+="$(printf " %q" -cpu host -smp cores=4 -m 2G")"
eval qemu-system-x86_64 "$qemu_options"