Search code examples
sedbsd

Issue assigning a SED to a variable


I am working on a BSD machine and what I am attempting to do is assign a variable the output from a SED command that uses a variable as input. Been working on this for 3 days, tried multiple different things, and always end up with './subscript: ${sed ...}: Bad substitution' error. Any help is greatly appreciated.

TMPEX=${sed "s/\\\\\\/\\\\/g" <<$TMPEX}

TEMPEX originally contains

C:\\Windows\\System32\\wininit.exe

and I would like to replace the double backslashes with single backslashes so that TMPEX contains:

C:\Windows\System32\wininit.exe

What am I missing?


Solution

    • Using sed and a shell which understands here-strings:

      TMPEX="$(sed 's/\\\\/\\/g' <<< "$TMPEX")"
      
    • Or, still using sed, for a shell which does not understand here-strings:

      TMPEX="$(echo "$TMPEX" | sed 's/\\\\/\\/g')"
      
    • Or even better, if the shell understands pattern substitution:

      TMPEX="${TMPEX//\\\\/\\}"