0

I want to replace in /usr/share/phpmyadmin/config.inc.php file

$cfg['blowfish_secret'] = '';

to

$cfg['blowfish_secret'] = 'blablabla';

with sed.

I am trying this:

sed -i "s/\$cfg['blowfish_secret'] = '';/$cfg['blowfish_secret'] = 'blablabla';/g" /usr/share/phpmyadmin/config.inc.php

but it doesn't change. How can I do this?

Dennis Williamson
  • 346,391
  • 90
  • 374
  • 439
iugurbas
  • 3
  • 1
  • Invert double and single quotes. (rewrite your file with double quotes, Put your sed command between single quotes.). You can do `sed -i 'y/'"'"'/"/;s/\$cfg\["blowfish_secret"\] = "";/$cfg["blowfish_secret"] = "blablabla";/g' /usr/share/phpmyadmin/config.inc.php` (obviously, it's only possible if there's nothing that can be expanded between double quotes in your php file for the php parser); – Casimir et Hippolyte Aug 09 '19 at 21:28
  • Why not rewrite your php config files like templates with placeholders. Something like: `$cfg['blowfish_secret'] = '{bfsvalue}';`. This way you can easily find and replace with the correct value. – Casimir et Hippolyte Aug 09 '19 at 21:34

2 Answers2

2

You need to do some more escaping:

sed -i "s/\$cfg\['blowfish_secret'\] = '';/\$cfg['blowfish_secret'] = 'blablabla';/" /usr/share/phpmyadmin/config.inc.php

Since your command is in double quotes to accommodate the single-quoted strings in the data, you will need to escape both dollar signs so that $cfg doesn't get interpreted as a variable by the shell.

You need to escape the square brackets on the left hand side so the enclosed string isn't treated as a bracket expression (group of characters to match) by sed.

Dennis Williamson
  • 346,391
  • 90
  • 374
  • 439
1

This might work for you (GNU sed):

sed 's/\(\$cfg\['\''blowfish_secret'\''\] = '\''\)\('\'';\)/\1blablabla\2/' file

This is a good example of:

For the third problem; most people (it seems) prefer to surround the whole of the sed command by double quotes. This solves the problem of single quotes but opens up a side effect that the command can be interpreted by the shell. If you are sure that these side effects will not rear their ugly head (variables and shell history can catch one out) go ahead, but by surrounding the sed command by single quotes and:

  • replacing a single quote by '\''
  • inserting \ before any literal [,],*,^,$ and \

will most likely, save the day.

potong
  • 55,640
  • 6
  • 51
  • 83
  • @Jotne I feel your pain, life can seem hard but please read my answer carefully. Surrounding sed commands by single quotes and obeying the simple laws? I included, will in the long run benefit both you and the maintainer following you. – potong Aug 10 '19 at 07:55