Use printf
(1):
x='a real \good %* load of c$rap'
x=$(printf '%q' "$x")
echo $x
will return
a\ real\ \\good\ %\*\ load\ of\ c\$rap
Use printf
(1):
x='a real \good %* load of c$rap'
x=$(printf '%q' "$x")
echo $x
will return
a\ real\ \\good\ %\*\ load\ of\ c\$rap