sed - with escape character

i have string as below

str=".<date>"

in which i need to replace < with /< , when i tried with sed , got the output.

--> echo $str | sed 's/</\\</g' 
.\<date>

when i tried to assign it to a variable , i am not getting the same

--> a=`echo $str | sed 's/</\\</g'` ; echo $a
.<date>

can you guys help me out , i m hitting :wall:..

thanks in advance

Try:

echo "$a"

tried it, but thats not working. even with IFS="" , its failing . :frowning:

It is because of the back ticks. Try:

a=$(echo "$str" | sed 's/</\\</g')
echo "$a"

or

a="${str%%<*}\\<${str#*<}"

or (bash/ksh93)

a=${str/</\\<}

--
with back ticks:

a=`echo "$str" | sed 's/</\\\</g'`

This is one of the reasons I avoid back ticks..

1 Like

got it .. thanks:b: