I'm trying to understand a sed command shown on ETA Lab's Rich’s sh (POSIX shell) tricks page, specifically in the trick Shell-quoting arbitrary strings:
quote () { printf %s\\n "$1" | sed "s/'/'\\\\''/g;1s/^/'/;\$s/\$/'/" ; }
In the third sed command (\$s/\$/'/), I understand the first $ is escaped to prevent the shell to interpret it as a hypothetical $s variable, so it can be passed to sed and be rightfully interpreted as the last line address for the s command, but why escape the second $ ? Shouldn't it correctly match the end of the line as-is ?