Escaping dots in domain names with sed in Bash

I am trying to save the return of a sed substitution in a variable:

  • D=domain.com echo $D | sed 's/\./\\./g' 

    Correctly returns: domain \ .com

  •  D1=`echo $D | sed 's/\./\\./g'` echo $D1 

    Returns: domain.com

What am I doing wrong?

+6
source share
2 answers
  D2 = `echo $ D |  sed 's /\./\\\\./ g'`
 echo $ D2 

Think of shells checking a line every time it is executed. So echo $ D1, which has escape sequences in it, has the value escapes applied to the value when the string is parsed before echo sees it. The solution escapes even more.

Getting the right vertices on nested shell operations can make you live in interesting moments.

+8
source

The backtick operator replaces the escape backslash with a backslash. You need to exit twice:

 D1=`echo $D | sed 's/\./\\\\./g'` 

You can also avoid the first backslash if you want.

+2
source

Source: https://habr.com/ru/post/895039/


All Articles