1

I am writing a bash script to rename icons. Specifically I am trying to change "icon-close-m" to "icon-close icon-size-medium" in HTML files. (though it could contain any substring besides "close", as noted by the [a-z] in the line below)

It works when I run this command from the command line:

sed -i -e "s/\(icon-[a-z]*\)-m[^e]/\1 icon-size-medium/g" ./app/index.html

Result:

icon-close icon-size-medium

But, when I run it from the script it changes it to this:

icon-close icon-sizeedium

Here is an example of text that should change:

<span class="icon-close-m"></span>

Should become:

<span class="icon-close icon-size-medium"></span>

It appears to be going through and replacing the -m in the new replacement, after the initial replacement. It is only doing this when the command is implemented in a bash script. I have not seen this happen before and I haven't seen any similar issues on SE/SO. Note that this command is not inside of any sort of loop.

  • 1
    Sure thing @steeldriver. Just edited. As far as I can tell, it goes through and adds icon-size-medium to the string. because that string has the letter e followed by -m, I believe it is replacing it because it matches the regex requirement. – John Mar 18 at 23:02
  • 1
    I wonder if you have some stray control characters in the sed expression in the script? You would get that result if the replacement text was /\1 icon-size-m^H^Hedium/ where ^H is a literal backspace character. – Kusalananda Mar 18 at 23:21
  • 1
    @Kusalananda That's a good call. How can I check for/remove stray control characters? – John Mar 18 at 23:27
  • 1
    The simplest would be to just retype the command in the script. Otherwise, it would depend on your editor. You could also use od -a on the script to see if you can spot any odd characters. – Kusalananda Mar 18 at 23:30

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.