1

I've got some issues with escaping parentheses in a string, using bash and sed.

Here's what I'm doing:

#!/bin/bash 
olddescription='(1 phrase/line)'
newdescription="\"$volledigenaam\""
(cd /home/hew/git/odoo/addons/$technischenaam ; sed -i s/$olddescription/$newdescription/g __openerp__.py)

I've read that if I use single quotes, I don't need to escape the parentheses. I also tried escaping the parentheses with backslash but didn't work either.

This is the error:

sed: -e expression #1, char 4: unterminated `s' command

Any ideas on how to solve this?

RobbeM
  • 727
  • 7
  • 16
  • 36
  • The single quotes are not part of the variable value so have no effect in the sed command. Try adding a "set -x" at the beginning and you should see how things get expanded. – Wayne Vosberg Jul 28 '17 at 16:03
  • BTW, [BashFAQ #21](http://mywiki.wooledge.org/BashFAQ/021) is on-point for what you're trying to do here. See `gsub_literal` defined therein. – Charles Duffy Jul 28 '17 at 16:22

1 Answers1

0

Scape the slash in the value of olddescription:

olddescription='(1 phrase\/line)';

Then, you can try this: sed -i "s/$olddescription/$newdescription/g" __openerp__.py

jonathadv
  • 346
  • 2
  • 10
  • Note that `olddescription` has a `/` in it. Adding syntactic quotes, while necessary, isn't sufficient to prevent `sed` from getting confused in that scenario. – Charles Duffy Jul 28 '17 at 16:20
  • Thanks for correcting me @CharlesDuffy. I changed my answer to solve the real issue. – jonathadv Jul 28 '17 at 16:49
  • Test your modified answer: `olddescription='(1 phrase/line)'; newdescription="\\\"$volledigenaam\\\""; sed -e "s/$olddescription/$newdescription/g" – Charles Duffy Jul 28 '17 at 16:57
  • Thanks again! Fixed and retested. I missed the slash in the `olddescription`. Sorry for this. – jonathadv Jul 28 '17 at 17:08
  • Are the literal backslashes before the quotes actually needed? That is to say, if we *only* escaped the one in `olddescription` and made no other changes, would that not fix the OP's issue as well? (This also doesn't help if the value of `$volledigenaam` contains a `/`; since it isn't specified by the OP, an *ideal* answer would cover all possible values). – Charles Duffy Jul 28 '17 at 17:14
  • You're totally right! Thanks for making me to see it! Answer fixed. – jonathadv Jul 28 '17 at 17:24
  • Thanks guys, worked perfectly! – RobbeM Jul 31 '17 at 11:08
  • The so-called duplicate is overly broad and does not easily answer this simple question. – Ray Salemi Jan 26 '22 at 12:57