I am trying to keep 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?
D2=`echo $D | sed 's/\./\\\\./g'` echo $D2
Think of shells rescanning the line each time it is executed. Thus echo $D1, which has the escapes in it, have the escapes applied to the value as the line is parsed, before echo sees it. The solution is yet more escapes.
Getting the escapes correct on nested shell statements can make you live in interesting times.
The backtick operator replaces the escaped backslash by a backslash. You need to escape twice:
D1=`echo $D | sed 's/\./\\\\./g'`
You may also escape the first backslash if you like.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With