I found this solution that works for me, but I don't understand how it does work given that the sed command encloses its own parameter in single quotes, and the whole ssh command is also passed in single quotes. This is being run from OSX (bsd).
ssh -t -p 22 root@thehost 'for user in $(/bin/ls -A1 /var/cpanel/users) ; do if grep -q RS=paper /var/cpanel/users/${user}; then sed -i 's/^RS=paper_lantern$/RS=x3/' /var/cpanel/users/${user}; fi; done'
Can someone explain to me why this works, and why I didn't need to escape the single quotes for the sed command - I can't find a reference anywhere? Thanks.