This is about the behaviour of the backspace (\b
) character.
I have the following C program:
int main() {
printf("Hello\b\b");
sleep(5);
printf("h\n");
return 0;
}
The output on my terminal is
Helho
with the cursor advancing to the first position of the following line.
First, the entire thing prints only after the 5 second sleep, so from that I deduced the output from the kernel to the terminal is line buffered.
So now, my questions are:
- Since the
\b\b
goes back two spaces, to the position of the (second)l
, then similar to howl
was replaced byh
, theo
should have been replaced by\n
. Why wasn't it? - If I remove the line
printf("h\n");
, it printsHello
and goes back two characters, without erasing. This I got from other answers is because of a non-destructive backspace. Why is this behaviour different for input and output? That is, if I input something into the terminal (even the very same program) and press Backspace, it erases the last character, but not for the output. Why?
I'm on an Ubuntu system on the xterm terminal using bash, if that helps.
Best Answer
No, the output from your program to the kernel is line-buffered. That's the default behaviour for
stdio
whenstdout
is a terminal. Add the callsetbuf(stdout, NULL)
to turn output buffering off forstdout
. Seesetbuf(3)
.Because the newline character just moves the cursor (and scrolls the screen), it doesn't print as a visible character that would take up the place of a glyph on the terminal. If we assume it would take the place of a glyph, what would it look like?
Well, what happens when you type depends on what mode the terminal is in. Roughly, it can be in the usual "cooked" mode where the terminal itself provides elementary line editing (handles backspaces); or in a "raw" mode, where all keypresses go to the application, and it's up to the application to decide what to do with them, and what to output in response. Cooked mode usually goes along with "local echo" where the terminal (local to the user) prints out the characters as they are typed. In raw mode, the application usually takes care of echoing the typed characters, to have full control over what's visible.
See e.g. this question for discussion on the terminal modes: What’s the difference between a “raw” and a “cooked” device driver?
If you run e.g.
cat
, the terminal will be in cooked mode (the default) and handle the line editing. Hitting for example xBackspaceCtrl-D will result incat
just reading the empty input, signalling the end of input . You can check this withstrace
. But if you run an interactive Bash shell instead, it will handle the backspace by itself, and output what it considers appropriate to do what the user expects, i.e. wipe one character.Here's part of the output for
strace -etrace=read,write -obash.trace bash
, after entering the mentioned sequence xBackspaceCtrl-D:First, Bash
read
s andwrite
s thex
, outputting it to the terminal. Then it reads the backspace (character code 0177 in octal or 127 in decimal), and outputs the backspace character (octal 010, decimal 8(*)) which moves the cursor back and outputs the control sequence for clearing the end of the line,<ESC>[K
. The last\4
is theCtrl-D
, which is used by Bash to exit the program.(* in input,
Ctrl-H
would have the decimal character code 8. Backspace is either the same or 127 as here, depending again on how the terminal is set up.)In comparison, the same experiment with
cat
only shows a single read of zero bytes, the "end of file" condition. End of file can mean either a connected pipe or socket being closed, an actual end of file, orCtrl-D
being received from a terminal in cooked mode:In particular,
cat
doesn't see thex
, nor the backspace, nor the actual code forCtrl-D
: they're are handled by the terminal. Which might be the virtual terminal driver in the kernel; an actual physical terminal over a serial connection or such; or a terminal emulator like xterm either running on the same machine or at the remote end of an SSH connection. It doesn't matter for the userspace software.