I am doing a find and replace with sed, replacing the BASH variable $a (when at the start of a new line) with the BASH variable $b:
$a
$b
This might work for you:
sed -i 'x;/^./{x;b};x;/^'"$a"'/{s//'"$b"'/;h}' file
Or:
sed -i ':a;$!{N;ba};s/^'"$a/$b"'/m' file