executable
executable

Reputation: 3590

Assign variable on a remote server

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

Upvotes: 0

Views: 51

Answers (1)

chepner
chepner

Reputation: 530960

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.

Upvotes: 1

Related Questions