I am trying to append a multiline text containing some single quotes(') to a file which is present on a different machine and can be only accessed through a different user. Here is my code snippet:
element_list=("element1" "element2")
for element in ${element_list[@]}
do
read -r -d '\n' CONTENT << EOM
field1 = on
field2 = '$element'
field3 = '$element-random-text'
EOM
CONFIG_FILE=/tmp/config.txt
sshpass -p vm_password ssh -t vm_user@<vm-ip> "sudo su - other_user -c 'echo \"$CONTENT\" >> $CONFIG_FILE'"
done
Though, the scripts execute successfully, the single quotes present in the field2 and field3 values disappear. The output appended to the file looks like:
field1 = on
field2 = element1
field3 = element1-random-text
field1 = on
field2 = element2
field3 = element2-random-text
I want those single quotes to dumped in the file too. Expected output:
field1 = on
field2 = 'element1'
field3 = 'element1-random-text'
field1 = on
field2 = 'element2'
field3 = 'element2-random-text'
Any help would be appreciated.
Note: There is a requirement to execute this command from a different machine and using the other_user(not the vm_user).