0

I want to preserve the backslash in the variable SET_TEST_MSG when using it in sudo su test -c [command].

When I run the following, I get r1rnrnrnrrnr instead of \r1\r\n\r\n\r\n\r\r\n\r:

SET_TEST_MSG='\r1\r\n\r\n\r\n\r\r\n\r'
sudo su test -c "/usr/bin/screen -dmS test bash -c \"echo $SET_TEST_MSG; exec bash\""
$PROG -n $PICKVM 0 -1 -d "$SET_TEST_MSG"

How do I preserve the backslash when running sudo su test -c [command]?

1 Answers1

3

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
ilkkachu
  • 138,973