Documentation
This likely is due to incorrect newline translation by your terminal and I guess that you don't see that behavior written into the log file you use but rather you see normal lines instead ... That is because terminals do post output processing before printing the output and that includes among other things translating LF
to CRLF
i.e. newline to carriage-return in order to bring each consecutive new line of output to the far left side of the terminal screen.
Post output processing should be enabled by default, but you can explicitly enable it with stty opost
and you can reproduce what you see if you disable output post processing like so:
$ stty -opost; seq 3; stty opost
1
2
3
$
... or you can reproduce it by only turning off terminal's output LF
to CRLF
translation onlcr
like so:
$ stty -onlcr; seq 3; stty onlcr
1
2
3
$
So, you could enable those two explicitly like:
$ stty opost onlcr; seq 3
1
2
3
$
... to fix that.
Notice that sometimes other terminal line printing settings might be involved in that and you might want to give combined settings like -nl
a try ... To see current settings in your terminal, run stty -a
like so:
$ stty -a
speed 38400 baud; rows 24; columns 80; line = 0;
intr = ^C; quit = ^\; erase = ^?; kill = ^U; eof = ^D; eol = <undef>;
eol2 = <undef>; swtch = <undef>; start = ^Q; stop = ^S; susp = ^Z; rprnt = ^R;
werase = ^W; lnext = ^V; discard = ^O; min = 1; time = 0;
-parenb -parodd -cmspar cs8 -hupcl -cstopb cread -clocal -crtscts
-ignbrk -brkint -ignpar -parmrk -inpck -istrip -inlcr -igncr icrnl ixon -ixoff
-iuclc -ixany -imaxbel iutf8
opost -olcuc -ocrnl onlcr -onocr -onlret -ofill -ofdel nl0 cr0 tab0 bs0 vt0 ff0
isig icanon iexten echo echoe echok -echonl -noflsh -xcase -tostop -echoprt
echoctl echoke -flusho -extproc
... See man stty
for more.
Discussion
What is the exact reason for that on your system? ... That can happen for a multitude of reasons and can only be traced on your system which I don't currently have hold of but, you do.
Why with sudo
specifically? ... Well sudo
gets its own terminal device "pseudo" (mainly to receive input for password … See https://askubuntu.com/a/1449987 ) while it's running your script (especially if it runs in its own shell like when you run your script with sudo
without specifying its -b, --background
option):
$ sudo tty; tty
/dev/pts/1
/dev/pts/0
$
What does that mean? It means this:
$ { sudo sh -c 'stty -onlcr; seq 3'; seq 4 6; } | tee file
1
2
3
4
5
6
$
$
$ cat file
1
2
3
4
5
6
... notice disabling onlcr
under sudo
doesn't affect the current terminal's commands output printing but affects those in sudo
's ... Also notice that the same output written to the file is not affected as, in reality and in its essence, it's the same:
$ cat -A file
1$
2$
3$
4$
5$
6$
Your use case
It happens after I ask the user to enter input on the command line
(press ENTER).
... yes, it does:
$ sudo bash -c 'for i in 1 2 3; do read -t1; echo "$i"; done' | tee
1
2
3
$
... but, starting from the version of sudo
used on Ubuntu 23.10, this particularity seems to be fixed (this seems to only affect output when it's sent to a none tty e.g. a pipe ... See source code) ... Special thanks to @DanielT for testing and bringing it to my attention.