I've read a bunch of other questions about shell quoting and spaces in file names, and the suggestions don't seem to be working in this case.
Putting a variable with a string with an escaped space inside of another variable un-escapes the space in the final output.
This script assembles a command from user input and hard-coded values, displays the command stored in the variable, then runs it:
#!/bin/bash
memory="4096"
cpus="--cpu=host --vcpus=2"
type="--os-type=linux"
variant="--os-variant=debiantesting"
graphics="--graphics spice"
network="--network bridge=vmbr0"
disk="--disk path=/dev/$chosen_vg_name/$lv_name"
#This line is the problem:
location="--cdrom /share/media/Linux\ ISO/debian11-server.iso";
# Run the command based on the supplied variable values:
install_cmd="virt-install -n $vm_name --memory $memory $cpus $type $variant $disk $location $graphics $network";
printf "$install_cmd\n";
$install_cmd;
exit 0;
I understand what is happening. The space in the file path in the location
variable isn't getting escaped when it's added to the install_cmd
variable, so the virt-install
command thinks ISO/debian11-server.iso
is an unknown argument.
I have also tried:
location='--cdrom /share/media/Linux ISO/debian11-server.iso';
location="--cdrom "/share/media/Linux ISO/debian11-server.iso"";
location='--cdrom /share/media/Linux\ ISO/debian11-server.iso';
location='--cdrom /share/media/Linux\\ ISO/debian11-server.iso';
location="--cdrom /share/media/Linux\\ ISO/debian11-server.iso";
What is the correct syntax to escape the space in the path name in the location
variable so that it stays escaped when location
is added to the variable containing the final command?