printf '%s\t%s\n' foo bar,
printf does output
r are single-width graphical characters.
Upon receiving those characters, the terminal will display a corresponding glyph and move the cursor one column to the right, unless it's already reached the right edge of the screen (paper in original tele-typewriters)), in which case it may feed a line and return to the left edge of the screen (wrap) or just discard the character depending on the terminal and how it's been configured.
<LF> are two control characters.
<LF> (aka newline) is the line delimiter in Unix text, but for terminals, it just feeds a line (move the cursor one position down). So the terminal driver in the kernel will actually translate it to
<CR> (return to the left edge of the screen),
<LF> (cursor down) (
stty onlcr generally on by default).
<Tab> tells the terminal to move the cursor to the next tab stop (which on most terminals are 8 positions apart by default but can also be configured to be set anywhere) without filling the gap with blanks.
So if those characters are sent to a terminal with tab stops every 8 columns whilst the cursor is at the start of an empty line, that will result in:
printed on the screen at that line. If they are sent whilst the cursor is in third position in a line that contains
xxxxyyyyzzzz, that will result in:
On terminals that don't support tabulation, the terminal driver can be configured to translate those tabs to sequences of spaces. (
The SPC character, in original tele-typewriters would move the cursor to the right, while backspace (
\b) would move it to the left. Now in modern terminals, SPC moves to the right and also erases (writes a space character as you'd expect). So the pendant of
\b had to be something newer than ASCII. On most modern terminals, it's actually a sequence of characters:
There are more escape sequences to move
n characters left, right, up, down or at any position on the screen. There are other escape sequences to erase (fill with blank) parts of lines or regions of the screen, etc.
Those sequences are typically used by visual applications like
dialog where text is written at arbitrary positions on the screen.
Now, all X11 terminal emulators and a few other non-X11 ones like GNU
screen let you select areas of the screen for copy paste. When you select a part of what you see in the
vi editor, you don't want to copy all the escape sequences that have been used to produce that output. You want to select the text you see there.
For instance if you run:
Which simulates an editor session where you enter
abC, go back to the beginning, replace
B, move to the next tab stop, then one more column to the right, then two columns to the left, then enter
ABC, a 4 column gap and
If you select that with the mouse in
putty, they will store in the selection
ABC, 4 space characters and
What ends up in the selection is what has been sent by
printf but post-processed by both the terminal driver and the terminal emulator.
For other kinds of transformation, see the
e followed by a combining acute accent) changed to
é the pre-composed form) by
echo abc that ends up being translated to
ABC by the terminal driver before sending to the terminal after a
<LF> is one of those few control characters that are actually sometimes found in text files (also
<CR> in MSDOS text files, and sometimes
<FF> for page break).
So some terminal emulators do choose to copy them when possible in the copy-paste buffers to preserve them (that's generally not the case of
For instance, in VTE-based terminals like
gnome-terminal, you may see that, when you select the output of
printf 'a\tb\n' on an empty line,
gnome-terminal actually stores
a\tb in the X11 selection instead of
a, 7 spaces and
But for the output of
printf 'a\t\bb\n', it stores
a, 6 spaces and
b, and for
a, 7 spaces and
There are other cases where the terminals will try to copy the actual input, like when you select two lines after running
printf 'a \nb\n' where that invisible trailing space will be preserved. Or when selecting two lines doesn't include a LF character when the two lines result from wrapping at the right margin.
Now, if you want to store the output of
printf into the CLIPBOARD
X11 select, best is to do it directly like with:
printf 'foo\tbar\n' | xclip -sel c
Note that when you paste that in
xterm or most other terminals,
xterm actually replaces that
\r because that's the character
xterm sends when you press Enter (and the terminal driver may translate it back to