1

Why am I losing shell command output? (in this case via ssh to an 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.

^^^ does not print 111. It seems like the first line is being lost:

$ 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 makes no difference)

It works fine from on the host:

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

1 Answers1

4

You're losing output because the quoting is wrong.

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

What you have here is a call to bash with a requirement to execute echo. The remaining argument 111 is offered to bash but unused. (The result is a blank line.)

What you probably want is one of the following alternatives.

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

Chris Davies
  • 116,213
  • 16
  • 160
  • 287
  • specifically in the case of SSH, since it joins all the args with spaces and passes that string to the remote shell – ilkkachu Sep 01 '22 at 10:34