You're asking su
to run the command through a shell, and then running another shell to go. You'll need to make sure to escape everything properly for the inner shell to see the command you want it to see.
This is similar to the case here: Quoting in ssh $host $FOO and ssh $host "sudo su user -c $FOO" type constructs
However, I'm not sure why you're running sudo
and su -c
there, when you could just do the same with sudo
, removing one shell and making the escaping much easier:
msg='\r1\r\n\r\n\r\n\r\r\n\r'
sudo -u test /usr/bin/screen -dmS test bash -c "echo '$msg'; exec bash"
Alternatively, put the innermost command in a script file, and run that instead. (Just make sure the target user can run the script.)
$ cat > /tmp/msgshell.sh <<'EOF'
#!/bin/bash
msg='\r1\r\n\r\n\r\n\r\r\n\r'
echo "$msg"
exec bash
EOF
$ chmod 755 /tmp/msgshell.sh
$ sudo -u test /usr/bin/screen -dmS test /tmp/msgshell.sh