5

I'm having a problem with using sed in a bash script. Here is the line:

sed -i "s/"$name"/"$input"/g" ~/input.script

I'm getting this error: sed: -e expression #1, char 24: unterminated `s' command

Weirdly enough it was working in an earlier version of my code. I've been trouble shooting but I cannot find a solution.

Kev
  • 51
  • 2
  • 3
    Your variables probably have a newline or slash in them which is confusing sed. – Etan Reisner Jul 07 '15 at 18:54
  • @EtanReisner I'm an idiot. That ended up being the error. There was a space in the variable. – Kev Jul 07 '15 at 18:56
  • 1
    A space only causes a problem with unquoted variables. So if you removed them like Eugeniu Rosca said then the problem should have gone away. A newline would still be a problem though as would a slash. – Etan Reisner Jul 07 '15 at 18:57
  • 2
    See http://stackoverflow.com/questions/29613304/is-it-possible-to-escape-regex-metacharacters-reliably-with-sed/29626460#29626460 – Ed Morton Jul 07 '15 at 19:13

1 Answers1

6

You don't need nested quotes. Also, try changing the delimeter:

sed -i "s@$name@$input@g" ~/input.script
Eugeniu Rosca
  • 5,177
  • 16
  • 45
  • 4
    The use of `@` here is to allow `/` in the variable values without a problem. It trades the problem for `@` in the variable values. So you need to pick something that cannot be in your input to do this correctly. – Etan Reisner Jul 07 '15 at 19:12
  • @Etan Reisner: Yep, I am aware of it :) Still thanks for mentioning. Anyways, it seems like OP solved his issue (waiting for the explanations...). – Eugeniu Rosca Jul 07 '15 at 19:15
  • Yeah, that wasn't for you that was for the OP/etc. – Etan Reisner Jul 07 '15 at 19:16