Search code examples
regexbashperlreplacequoting

Bash find and replace string


I am writing a code that will change the IP of VestaCP panel automatically. I need the code to replace the variable $oldIP with a new variable $newIP, so I wrote this piece of code.

sudo perl -pi -e 's/${oldIP}/${newIP}/g' /etc/nginx/conf.d/${oldIP}.conf

But the code doesn't replace the oldIP from oldIP.conf (123.123.123.123.conf).

However, if I try

sudo perl -pi -e 's/123.123.123.123/123.456.123.456/g' /etc/nginx/conf.d/123.123.123.123.conf

It works flawlessly, although I can't use this code since ever servers have a different IP and I can't predict the next IP when it changes.

What am I doing wrong here? Any solutions? Thanks in advance!

P.S. If it helps to know, I am on Ubuntu 16.04.


Solution

  • Your variables are not expanded between single quotes. Use double quotes instead:

    sudo perl -pi -e "s/${oldIP}/${newIP}/g" /etc/nginx/conf.d/${oldIP}.conf