I'm trying to execute an command via sh -c over SSH in an LXC container. Since lxc-attach is involved I have to use sh -c
like this: ssh <host> "lxc-attach -- sh -c \"<command>;<command>\""
. This works for most cases. But when I try to use a literal environment variable in the command, the variable keeps expanding.
The problem can be simplified to this: I want to print a literal $HOME
Local:
user@local:~$ sh -c 'echo $HOME'
/home/user
# Despite single quote the variable gets expanded, one has to escape the $ sign
user@local:~$ sh -c 'echo \$HOME'
$HOME
So far so good, but when I try this over SSH remote:
user@local:~$ ssh remote "sh -c 'echo \$HOME'"
/root
user@local:~$ ssh remote 'sh -c 'echo \$HOME''
user@local:~$ ssh remote "sh -c 'echo $HOME'"
/root': 1: Syntax error: Unterminated quoted string
I tried serveral variants of escaping the variable. It keeps expanding, nothing is printed or an error appears.