Linux

ssh <host> bash -c ‘cmd’ ,為什麼第一行輸出失去了?

  • September 1, 2022

為什麼我會失去 shell 命令輸出?(在這種情況下,通過 ssh 連接到 Ubuntu Raspberry Pi)

$ ssh pi@192.168.4.1 bash -l -c 'echo 111'

SSH is enabled and the default password for the 'pi' user has not been changed.
This is a security risk - please login as the 'pi' user and type 'passwd' to set a new password.

^^^ 不列印 111。第一行似乎失去了:

$ ssh pi@192.168.4.1 bash -l -c 'echo 111 && echo 222'

SSH is enabled and the default password for the 'pi' user has not been changed.
This is a security risk - please login as the 'pi' user and type 'passwd' to set a new password.


222

(-l 沒有區別)

它可以在主機上正常工作:

pi@raspberrypi:~ $ bash -c 'echo 111'
111

您正在失去輸出,因為引用錯誤。

ssh pi@192.168.4.1 bash -l -c 'echo 111'

你在這裡bash有一個需要執行的呼叫echo。剩餘的參數111提供給bash但未使用。(結果為空行。)

您可能想要的是以下替代方案之一。

ssh pi@192.168.4.1 'echo 111'                 # Shell executing echo

ssh -t pi@192.168.4.1 'echo 111'              # Interactive shell (with `.bashrc`)  executing echo

ssh pi@192.168.4.1 'bash -l -c "echo 111"'    # Shell calling login shell (`.bash_profile`) to execute echo

引用自:https://unix.stackexchange.com/questions/715686