title: sed with regular expression as a bash variable
body: <p>We have an application that keeps some info in an encrypted file. To edit the file we have to put the text editor name in an environment variable in bash, for example, EDITOR=vi. Then we run the application and it opens the decrypted file in vi. I am trying to come up with a bash script that updates the encrypted file. The only solution that I can think of is passing sed command instead of vi to the EDITOR variable. It works perfectly for something like <code>EDITOR='sed -i s#aaaa#bbbb#'</code>.</p>
  <p>Problem starts when I need space and regular expression. For example: <code>EDITOR='sed -i -r &quot;s#^(\s*masterkey: )(.*)#\1xxxxx#&quot;'</code>  which return error. I tried running the EDITOR in bash with <code>$EDITOR test.txt</code> and I can see the problem. It doesn't like double quotes and space between them so I added a backslash before the double quotes and \s instead of space. Now it says unterminated address regex. For several hours I googled and couldn't find any solution. I tried replacing single quotes with double quotes and vice versa and everything that I could find on the internet but no luck.</p>
  <p>How can I escape and which characters should I escape here?</p>
  <p><strong>Update:</strong><br />
  Maybe if I explain the whole situation somebody could suggest an alternative solution. There is an application written by Ruby and it is inside a container. Ruby application has a secret_key_base for production and we supposed to change the key with <code>EDITOR=vi rails credentials:edit --environment=production</code>. I don't know Ruby and google did not return any ruby solution for automation so I could only think about sending sed instead of vi to Ruby.</p>