Tuesday, February 6, 2024

[SOLVED] Variable not storing values over ssh

Issue

I need to run some commands over ssh to a remote machine. But while trying to execute some basic commands such as:

ssh [email protected] "a='hello'; echo $a"

produces no output on the screen. However

ssh [email protected] "echo 'hello'"

is working fine with the output

Hello

can anyone explain me why this is happening ?

Also now the major issue is that I need to execute awk/cut onto the remote machines and store there outputs in a variable. Something like :

ssh [email protected] 'a=$(df /tmp | awk 'NR==2 {print $NF}');echo $a' 

But I am unable to place ' inside ' ' .


Solution

ssh [email protected] 'a="hello"; echo $a' will be fine in your command, $a will be interpreted in local shell, before execute ssh.

an other way is ssh [email protected] "a='hello'; echo \$a"



Answered By - Shen Yudong
Answer Checked By - Robin (WPSolving Admin)