Here's the problem.
I'm trying as "root" user to execute a command as "myusername" user.
That's why I'm using su - $USERNAME -c <command>
The <command> itself is something like sudo sed -i 's|SEARCH_REGEX|REPLACEMENT|' /etc/cloud/cloud.cfg
But each time I execute my script I got an error which seems to be due to the REPLACEMENT part.
I guess this is something around carriage return or spaces.
Here's the code:
#!/bin/bash
$USERNAME="myusername"
$USERPWD="p@ssw0rd123"
PATTERN='s|preserve_hostname:\sfalse|preserve_hostname: true\nmanage_etc_hosts: false|'
su - $USERNAME -c 'sudo -S bash -c "sed -i '$PATTERN' /etc/cloud/cloud.cfg"' <<< $USERPWD
And here's the error I get:
true\nmanage_etc_hosts:: -c: line 0: unexpected EOF while looking for matching `"'
true\nmanage_etc_hosts:: -c: line 1: syntax error: unexpected end of file
The ressources that helped me:
https://linuxize.com/post/how-to-use-sed-to-find-and-replace-string-in-files/
https://linuxhint.com/50_sed_command_examples/
What am I doing wrong ?