I am trying to update the following line, which appears in a PHP config file, from a shell script that updates the application that is associated with the config file:
$config['version'] = "v2.7.0";
When I run my shell script I pass in the version number to be updated. The shell script includes the following command to search for and replace the line:
sudo sed -i -e '$a$config['version'] = "v$1";' /var/www/app/application/config/production/config.php
Running that results in the following:
$config[version] = "v$1";
I’ve tried all sorts of combinations of escaping quotes but nothing seems to work. Does anyone know what combination will work that keeps the quotes and expands the variable that is passed in? Alternate approaches that will work in a shell script are welcome!
2
Answers
Use double quotes as opposed to single ones and escape the double quotes as well as $
Assuming the value to change is in the form
and you want to get
you can use a sed command (w/o escaping) similar to this:
The former will not break when the v1 contains spaces.
Explanation: