If I understand correctly \b
escape sequence moves the active cursor position to the left and \n
inserts a newline at the cursor position. But the following example is confusing.
λ> cat hello.c #include <stdio.h> int main() { printf("hello,world\b\b\b\b\bWOR"); return 0; } λ> cc hello.c && ./a.out hello,WORλ> λ> cat hello.c #include <stdio.h> int main() { printf("hello,world\b\b\b\b\bWOR\n"); return 0; } λ> cc hello.c && ./a.out hello,WORld λ>
In the first example, \b\b\b\b\b
moves the cursor five positions to the left (after ,
) and inserts W
followed by O
and R
and the characters in the original string after ,
are omitted. But, in the second example, usage of \n
alters the behaviour of b
in an unexpected way. The characters in the original string are overwritten and \n
is inserted at the end, rather than at the cursor position. Can someone please explain this behaviour? (Or is it terminal dependent? I have tried on two different terminals.)
\b
to a backspace character and of\n
to a new-line character occurs during program translation (compilation). – Eric Postpischil