0

a.txt:

^variable=test\n

a.sh

#!/bin/ksh  
string=aAxdf\ndafsfasdf\n   
sed -i '/\^variable=/c\^variable='"$string"'' a.txt

output: a.txt contains below contents without slash

^variable=aAxdfndafsfasdfn

Expected output:

^variable=aAxdf\ndafsfasdf\n 

2 Answers2

0

Quote your string while assigning your variable and double your backslashes, so they won't be interpreted as a newline character by sed:

#!/bin/ksh  
string='aAxdf\\ndafsfasdf\\n'
sed -i '/\^variable=/c\^variable='"$string"'' a.txt
knittl
  • 246,190
  • 53
  • 318
  • 364
0

In your a.sh file, you will need to do the following:

  1. Quote your variable value being assigned.
  2. Escape the slash(es) with the escape character \

It would look like string='aAxdf\\ndafsfasdf\\n'

chilli10
  • 54
  • 1
  • 9