变量不通过 ssh 存储值

Variable not storing values over ssh

我需要 运行 通过 ssh 向远程机器发送一些命令。 但是在尝试执行一些基本命令时,例如:

ssh abc@remotemachine.com "a='hello'; echo $a"

屏幕上没有输出。 不过

ssh abc@remotemachine.com "echo 'hello'"

输出正常

Hello

谁能解释一下为什么会这样?

现在的主要问题是我需要在远程机器上执行 awk/cut 并将输出存储在一个变量中。 像 :

ssh abc@remotemachine.com 'a=$(df /tmp | awk 'NR==2 {print $NF}');echo $a' 

但我无法将 ' 放入 ' ' .

ssh abc@remotemachine.com 'a="hello"; echo $a'就可以了 在您的命令中,$a 将在本地 shell 中解释,然后再执行 ssh.

另一种方法是 ssh abc@remotemachine.com "a='hello'; echo $a"