Issue
I'm trying to assign a variable using SSH. I want to simply echo the content of the variable. Here is my little script :
#!/bin/bash
IP_PUBLIC="192.168.0.1"
ssh -oStrictHostKeyChecking=no root@"$IP_PUBLIC" '
COMMUNITY=$(uname -n);
echo '"$COMMUNITY"';
'
Everytime I run it it echo an empty line. I expect to echo the hostname of the machine
Solution
You just need to drop the inner single quotes.
#!/bin/bash
IP_PUBLIC="192.168.0.1"
ssh -oStrictHostKeyChecking=no root@"$IP_PUBLIC" '
COMMUNITY=$(uname -n);
echo "$COMMUNITY";
'
The outer single quotes already protect everything inside them from the local shell; the remote shell receives
COMMUNITY=$(uname -n);
echo "$COMMUNITY"
to execute, which is correctly quoted.
Answered By - chepner