What causes \r's to be inserted before \n's when retrieving a binary file over ssh, and how do I circumvent it?
It's the ONLCR
.c_oflag
termios setting which is causing the newline (\n
) to be turned into carriage-return/newline (\r\n
) by the pseudo-terminal allocated by ssh
on the remote machine (because of ssh's -t
option).
Turn it off with stty -onlcr
:
ssh -t me@there 'stty -onlcr; ...' > output
The official ASCII line ending is CR LF (i.e., return to line start and go to next line, "\r\n" in C-ish). To shave off a byte for each line (very important when yor memory is measured in KiB and disks are a few hundred MiB), Unix just uses '\n' to mark line end. Some systems (notably Microsoft) did go with the standard, so when moving text files among systems you have sometimes a translation task at hand.