Both the shell and the terminal are involved: most terminals that you will use do automatic line-wrapping. At startup, bash (in lib/readline/terminal.c) checks two termcap flags related to this:
_rl_term_autowrap = tgetflag ("am") && tgetflag ("xn");
which (refer to terminfo(5)) tell it if the terminal wraps lines in the special manner that VT100s (and related terminals) do:
auto_right_margin am am terminal has automatic
margins
eat_newline_glitch xenl xn newline ignored
after 80 cols (concept)
That's the case for Linux console and xterm. When bash sees this, it will helpfully wrap when it decides your cursor is at the right margin (to avoid problems with the newline glitch), as seen in display.c:
/* If we're at the right edge of a terminal that supports xn, we're
ready to wrap around, so do so. This fixes problems with knowing
the exact cursor position and cut-and-paste with certain terminal
emulators. In this calculation, TEMP is the physical screen
position of the cursor. */
At the same time, the terminal will wrap (by default) when it reaches the right margin. Using stty
to increase the size of the terminal to 100 columns will not help, if your terminal does not have 100 columns (and the terminal does not know what you told stty
). bash knows (or should, unless you have set the environment variable COLUMNS
).
But supposing that your terminal window actually has 100 columns...
If bash is confused about the line-length, it will misbehave in a manner matching the OP's comment
And the minimal example is open a serial connection and enter some keys until the line is full, then it gets broken and either continues on the next line or the same line just in the front.
This is the subject of several bug reports (which should be part of the bash FAQ, but the only aspect mentioned there is the issue with nonprinting characters in a prompt).
Given that comment, it appears that the connection "linux device via serial adapter" used cannot tell the shell reliably how wide the terminal is, so it defaults to 80 columns.
The comment about busybox appears to be off target, given the followup edit showing that the actual shell is bash. But the comment about "serial adapter" implies that you won't get window-size events passed to the shell. Reading bash's source-code, it appears that in this situation, it will only get a useful screen-size when first initializing (bash only executes _rl_get_screen_size initially, or in response to a SIGWINCH
).
But you can make a terminal description with the actual screen size, and use that in a subshell (which bash would use in the case where the system cannot supply a proper screensize):
#!/bin/sh
infocmp -1 | \
sed -e 's/^[^[:space:]].*|/fixed|/' \
-e '/lines#/d' \
-e '/cols#/d' \
>foo
resize -u | awk '
/COLUMNS=/{ sub("COLUMNS=","cols#"); }
/LINES=/{ sub("LINES=","lines#"); }
/export/{ next; }
{ sub(";",","); printf "\t%s\n", $0; }
' >>foo
tic foo
TERM=fixedsize bash
cat
)? What shell is running on the device (some BusyBox variant?)?