Linux – Using Variables in set-hostname and mv Command in Shell Script

hostnamelinuxmvshellshell-scripting

I have the below script in Shell:

read n
for ((i=1;i<=$n;i++))
do
echo "Connecting to $publicip"
ssh -i ./key.txt root@$publicip 'hostnamectl set-hostname autotest$i.domain.com && mv /etc/letsencrypt/live/autotest.domain.com /etc/letsencrypt/live/autotest$i.domain.com && reboot'
done

mv command makes use of a variable from the above commands. But it doesn't seem to be working. What I get an error is mv: cannot move '/etc/letsencrypt/live/autotest.domain.com' to a subdirectory of itself, '/etc/letsencrypt/live/autotest.domain.com/autotest.domain.com' Even set hostname also didnt seem to work (especially with variables), when I checked the other server to see if hostname is changed. Tried many things around variable like adding "" and {} etc, but nothing worked.

Can someone help me on this regard.

Best Answer

Shell variables are NOT expanded in single-quoted ' text. Use double quotes " for strings with shell variables.