1

I'm using below command to ssh from linux to windows server and run a client specific task which gives around 10-15 lines of output. Below command is giving correct output but only 5-6 lines of output only coming.

output=$(ssh -t username@ip 'command on')
echo $output

I tried tee log to pass the output to log file as well but still few lines of output only showing. Can anyone help me regarding this. Thanks.

  • 2
    Note that `echo $output` is prone to munge your output (combining it onto one line, replacing globs with filenames matching them, etc). Make it `echo "$output"` instead, as described in [I just assigned a variable, but `echo $variable` shows something else!](https://stackoverflow.com/questions/29378566/i-just-assigned-a-variable-but-echo-variable-shows-something-else) – Charles Duffy Nov 16 '20 at 18:08
  • 1
    Is the command printing on stderr as well as stdout? If so you can add `2>&1` to capture both: `output=$(ssh -t username@ip 'command on' 2>&1)`. – John Kugelman Nov 16 '20 at 18:12
  • 2
    BTW, if you don't trust your server to keep stdout open until it's closed if stdin shuts down early (which would be a bug, but this is a SSH server on Windows, so bugs wouldn't be surprising), consider something like `output=$(ssh username@ip 'command on' < <(sleep 5))`. If the `-t` is needed for correct behavior, that makes it _even more_ likely that the server side is buggy; commands that aren't intended to be interactively used by a human shouldn't need a TTY to work right. – Charles Duffy Nov 16 '20 at 18:14
  • @JohnKugelman 2>&1 is giving me only 1 line (last line from output) – Lokendra Singh Rathod Nov 16 '20 at 18:20
  • Thanks @CharlesDuffy. Putting output variable in quotes solved my issue. – Lokendra Singh Rathod Nov 16 '20 at 18:20
  • @CharlesDuffy Can you please write the answer so that I can accept it. Thanks. – Lokendra Singh Rathod Nov 16 '20 at 18:24
  • If I had to guess about why -- DOS newlines have `$'\r'` characters in them, sending the cursor back to the beginning of the line. UNIX doesn't recognize them as part of the newline sequence, and they aren't in the default `IFS` value, so when you just `echo $variable` without quotes, the newlines go away for the reasons given in the linked duplicate, and then the cursor keeps getting sent back to the beginning of the line, and consequently the same line gets overwritten over and over. – Charles Duffy Nov 16 '20 at 18:25
  • I closed this question as a duplicate of a preexisting question on the topic, so anyone looking at it is guided to follow the link (up at the top) to find the preexisting answers -- that way we have answers centralized in one place for each general question, so they can get as much attention as possible instead of being spread out and not as well-cared-for. – Charles Duffy Nov 16 '20 at 18:26

0 Answers0