Expand variables in sed

Just use double quotes instead of single quotes. You’ll also need to use {} to delimit the number_line variable correctly and escape the \, too.

sed -i.bak "${number_line}i\\$var1=$var2" $var3

I’d personally prefer to see all of the variables use the {}, ending up with something like:

sed -i.bak "${number_line}i\\${var1}=${var2}" ${var3}

Leave a Comment