Expand variables in a sed substitution on Bash

sed -i.bak ‘s/string_to_replace/’$variablename’/g’ filename

The string to be replaced is, as usual, unquoted. However the variable to be used is single quoted. Contrary to normal Bash practise, the single quote expands instead of being literal.

You are reading this post on Joel G Mathew’s tech blog. Joel's personal blog is the Eyrie, hosted here.