Here is a 'complicated for the sake of understanding' example. Hopefully it'll be helpful for anyone else trying to understand qutoes and backslashes.
In the example here, I want to repeat the pattern 3 times.
>~# echo 'ABC\n\t'
ABC\n\t
Using single quotes, it is fairly straightforward:
>~# echo 'ABC\n\t' | sed -e 's#ABC\\n\\t#ABC\\n\\tABC\\n\\tABC\\n\\t#'
↑ ↑ ↑ ↑ ↑ ↑ ↑
# These backslashes escaped by sed
ABC\n\tABC\n\tABC\n\t
Here is the way to do it using double quotes: (again, complicated just for the sake of understanding)
>~# echo 'ABC\n\t' | sed -e "s#ABC\\\n\\\t#$(printf '%0.sABC\\\\n\\\\t' $(seq 1 3))#"
↑ ↑ ↑ ↑ ↑ ↑
# These backslashes are removed in double quotes. Showing intermediate stage:
>~# echo 'ABC\n\t' | sed -e "s#ABC\\n\\t#$(printf '%0.sABC\\n\\t' $(seq 1 3))#"
# Expanding printf results in:
>~# echo 'ABC\n\t' | sed -e 's#ABC\\n\\t#ABC\\n\\tABC\\n\\tABC\\n\\t#'
ABC\n\tABC\n\tABC\n\t
Just to play around and master quotes and backslashes, replace the single quotes around printf with double quotes.
echo foo \
is equivalent toecho foo " "
. – chepner Oct 17 '17 at 16:39