This works in
bash, tested on Linux and OS X:
In general, for
$ followed by a string literal in single quotes
bash performs C-style backslash substitution, e.g.
$'\t' is translated to a literal tab. Plus, sed wants your newline literal to be escaped with a backslash, hence the
$. And finally, the dollar sign itself shouldn't be quoted so that it's interpreted by the shell, therefore we close the quote before the
$ and then open it again.
Edit: As suggested in the comments by @mklement0, this works as well:
What happens here is: the entire sed command is now a C-style string, which means the backslash that sed requires to be placed before the new line literal should now be escaped with another backslash. Though more readable, in this case you won't be able to do shell string substitutions (without making it ugly again.)