That's some complex quoting. The argument to
sed is built up from two parts. First, there's
's/$', a single-quoted string literal, yielding the characters
s/$. Then there's a double-quoted string, which contains the command substitution
`echo \\\r`. This runs the command
echo \r, which depending on the shell either prints
\r or a CR character. (The text printed by
echo ends in a newline, but the command substitution eats it up.) In order for this command to have the desired effect, you must be on a system where
echo \r prints a CR character, which I'll represent here as
The argument to sed is thus
s/$/␍/. This replaces every match of the regular expression
$ by the string
␍. The regex
$ matches the empty string, but only at the end of a line, so this sed command appends a CR to every line. Since a Unix line ends with LF while a Windows line ends with CR+LF, this transforms Unix line endings into Windows line endings.
GNU sed, but not other versions, understand backslash escapes such as
\r. So with GNU sed you can write
sed 's/$/\r/'. However, this doesn't work with other sed implementations (BSD, Solaris, …).
echo \r isn't very portable either. A portable solution is to use
tr, which has backslash escapes as a standard.
sed "$(echo 's/$/@/' | tr '@' '\r')"