How to print strings separated by TAB in bash?












8














I am trying to print two string separated by a TAB.
I have tried:



echo -e 'footbar'
printf '%st%sn' foo bar


Both of them print:



foo     bar


Where the whitespace between the two is actually 5 spaces (as per selecting the output with mouse in Putty).



I have also tried using CTRL+V and pressing TAB when typing the command, with the same result.



What is the correct way to force tab being printed as tab, so I can select the output and copy it to somewhere else, with tabs?



And the secondary question: why is bash expanding tabs into spaces?



Update:
Apparently, this is a problem of Putty:
https://superuser.com/questions/656838/how-to-make-putty-display-tabs-within-a-file-instead-of-changing-them-to-spaces










share|improve this question









New contributor




Asu is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
















  • 5




    Related: Output tab character on terminal window
    – steeldriver
    Dec 15 at 16:34










  • Why just not escape it? printf '%s\t%sn' foo bar
    – Valentin Bajrami
    Dec 15 at 17:11










  • @steeldriver Thanks that's very similar to what I need, but ultimately there isn't a solution...
    – Asu
    Dec 15 at 17:19






  • 1




    @Valentin That outputs footbar ...
    – wjandrea
    Dec 16 at 1:17






  • 1




    Despite that fact that you already know that you have an issue with your terminal: Bash by itself interprets $'t' as tabulator. So you can always concatenate strings like this - for example as assignment: v='This is'$'t''a test' And the print it literally, e.g. printf '%s' "$v"
    – rexkogitans
    Dec 17 at 7:11


















8














I am trying to print two string separated by a TAB.
I have tried:



echo -e 'footbar'
printf '%st%sn' foo bar


Both of them print:



foo     bar


Where the whitespace between the two is actually 5 spaces (as per selecting the output with mouse in Putty).



I have also tried using CTRL+V and pressing TAB when typing the command, with the same result.



What is the correct way to force tab being printed as tab, so I can select the output and copy it to somewhere else, with tabs?



And the secondary question: why is bash expanding tabs into spaces?



Update:
Apparently, this is a problem of Putty:
https://superuser.com/questions/656838/how-to-make-putty-display-tabs-within-a-file-instead-of-changing-them-to-spaces










share|improve this question









New contributor




Asu is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
















  • 5




    Related: Output tab character on terminal window
    – steeldriver
    Dec 15 at 16:34










  • Why just not escape it? printf '%s\t%sn' foo bar
    – Valentin Bajrami
    Dec 15 at 17:11










  • @steeldriver Thanks that's very similar to what I need, but ultimately there isn't a solution...
    – Asu
    Dec 15 at 17:19






  • 1




    @Valentin That outputs footbar ...
    – wjandrea
    Dec 16 at 1:17






  • 1




    Despite that fact that you already know that you have an issue with your terminal: Bash by itself interprets $'t' as tabulator. So you can always concatenate strings like this - for example as assignment: v='This is'$'t''a test' And the print it literally, e.g. printf '%s' "$v"
    – rexkogitans
    Dec 17 at 7:11
















8












8








8







I am trying to print two string separated by a TAB.
I have tried:



echo -e 'footbar'
printf '%st%sn' foo bar


Both of them print:



foo     bar


Where the whitespace between the two is actually 5 spaces (as per selecting the output with mouse in Putty).



I have also tried using CTRL+V and pressing TAB when typing the command, with the same result.



What is the correct way to force tab being printed as tab, so I can select the output and copy it to somewhere else, with tabs?



And the secondary question: why is bash expanding tabs into spaces?



Update:
Apparently, this is a problem of Putty:
https://superuser.com/questions/656838/how-to-make-putty-display-tabs-within-a-file-instead-of-changing-them-to-spaces










share|improve this question









New contributor




Asu is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.











I am trying to print two string separated by a TAB.
I have tried:



echo -e 'footbar'
printf '%st%sn' foo bar


Both of them print:



foo     bar


Where the whitespace between the two is actually 5 spaces (as per selecting the output with mouse in Putty).



I have also tried using CTRL+V and pressing TAB when typing the command, with the same result.



What is the correct way to force tab being printed as tab, so I can select the output and copy it to somewhere else, with tabs?



And the secondary question: why is bash expanding tabs into spaces?



Update:
Apparently, this is a problem of Putty:
https://superuser.com/questions/656838/how-to-make-putty-display-tabs-within-a-file-instead-of-changing-them-to-spaces







bash putty whitespace tabs






share|improve this question









New contributor




Asu is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.











share|improve this question









New contributor




Asu is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.









share|improve this question




share|improve this question








edited Dec 16 at 14:10









Jeff Schaller

38.4k1053125




38.4k1053125






New contributor




Asu is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.









asked Dec 15 at 16:10









Asu

1434




1434




New contributor




Asu is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.





New contributor





Asu is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.






Asu is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.








  • 5




    Related: Output tab character on terminal window
    – steeldriver
    Dec 15 at 16:34










  • Why just not escape it? printf '%s\t%sn' foo bar
    – Valentin Bajrami
    Dec 15 at 17:11










  • @steeldriver Thanks that's very similar to what I need, but ultimately there isn't a solution...
    – Asu
    Dec 15 at 17:19






  • 1




    @Valentin That outputs footbar ...
    – wjandrea
    Dec 16 at 1:17






  • 1




    Despite that fact that you already know that you have an issue with your terminal: Bash by itself interprets $'t' as tabulator. So you can always concatenate strings like this - for example as assignment: v='This is'$'t''a test' And the print it literally, e.g. printf '%s' "$v"
    – rexkogitans
    Dec 17 at 7:11
















  • 5




    Related: Output tab character on terminal window
    – steeldriver
    Dec 15 at 16:34










  • Why just not escape it? printf '%s\t%sn' foo bar
    – Valentin Bajrami
    Dec 15 at 17:11










  • @steeldriver Thanks that's very similar to what I need, but ultimately there isn't a solution...
    – Asu
    Dec 15 at 17:19






  • 1




    @Valentin That outputs footbar ...
    – wjandrea
    Dec 16 at 1:17






  • 1




    Despite that fact that you already know that you have an issue with your terminal: Bash by itself interprets $'t' as tabulator. So you can always concatenate strings like this - for example as assignment: v='This is'$'t''a test' And the print it literally, e.g. printf '%s' "$v"
    – rexkogitans
    Dec 17 at 7:11










5




5




Related: Output tab character on terminal window
– steeldriver
Dec 15 at 16:34




Related: Output tab character on terminal window
– steeldriver
Dec 15 at 16:34












Why just not escape it? printf '%s\t%sn' foo bar
– Valentin Bajrami
Dec 15 at 17:11




Why just not escape it? printf '%s\t%sn' foo bar
– Valentin Bajrami
Dec 15 at 17:11












@steeldriver Thanks that's very similar to what I need, but ultimately there isn't a solution...
– Asu
Dec 15 at 17:19




@steeldriver Thanks that's very similar to what I need, but ultimately there isn't a solution...
– Asu
Dec 15 at 17:19




1




1




@Valentin That outputs footbar ...
– wjandrea
Dec 16 at 1:17




@Valentin That outputs footbar ...
– wjandrea
Dec 16 at 1:17




1




1




Despite that fact that you already know that you have an issue with your terminal: Bash by itself interprets $'t' as tabulator. So you can always concatenate strings like this - for example as assignment: v='This is'$'t''a test' And the print it literally, e.g. printf '%s' "$v"
– rexkogitans
Dec 17 at 7:11






Despite that fact that you already know that you have an issue with your terminal: Bash by itself interprets $'t' as tabulator. So you can always concatenate strings like this - for example as assignment: v='This is'$'t''a test' And the print it literally, e.g. printf '%s' "$v"
– rexkogitans
Dec 17 at 7:11












3 Answers
3






active

oldest

votes


















9














Like ilkkachu said, this isn't an issue with bash, but with the terminal emulator which converts tabs to spaces on output.



Checking different terminals, putty, xterm, and konsole convert tabs to spaces, while urxvt and gnome-terminal do not. So, another solution is to switch terminals.






share|improve this answer



















  • 3




    It can also be done by the tty driver after you run stty tab3.
    – Stéphane Chazelas
    Dec 15 at 19:09



















14















the whitespace between the two is actually 5 spaces.




No, it's not. Not in the output of echo or printf.



$ echo -e 'footbar' | od -c
0000000 f o o t b a r n
0000010



What is the correct way to force tab being printed as tab, so I can select the output and copy it to somewhere else, with tabs?




This is a different issue. It's not about the shell but the terminal emulator, which converts the tabs to spaces on output. Many, but not all of them do that.



It may be easier to redirect the output with tabs to a file, and copy it from there, or to use unexpand on the output to convert spaces to tabs. (Though it also can't know what whitespace was tabs to begin with, and will convert all of it to tabs, if possible.) This of course would depend on what, exactly, you need to do with the output.






share|improve this answer























  • I meant that when I try to select the output, it is being treated as 5 spaces. Thanks for the 'od -c' to verify the contents of the command output.
    – Asu
    Dec 15 at 17:14






  • 1




    @Asu I think he understands that. His solution is to get the output via other means since the terminal emulator is not guaranteed to leave tabs as tabs when you select them in the window. However, I just checked and while putty, xterm, and konsole convert tabs to spaces, urxvt and gnome-terminal do not. So, another solution is to switch terminals.
    – JoL
    Dec 15 at 17:47












  • @JoL Yes, that's the conclusion I just came to a minute ago, and I think it would be the accepted answer if somebody cares to post it as such...
    – Asu
    Dec 15 at 17:50






  • 1




    @Asu, yeah, I thought about just working around the issue manually. It would be annoying to have to do that, but then I admit I hadn't realized that there are terminal emulators that do support copying tabs. Changing to one that does, would of course be a much better solution!
    – ilkkachu
    Dec 15 at 22:16



















4














In printf '%st%sn' foo bar, printf does output foo<TAB>bar<LF>.



f, o, b, a and 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.



<Tab> and <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:



foo     bar


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:



xxfooyyybarz


On terminals that don't support tabulation, the terminal driver can be configured to translate those tabs to sequences of spaces. (stty tab3).



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: <Esc>, [, C.



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 vi, lynx, mutt, 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:



printf 'abCrACbBte[CbbDn'


Which simulates an editor session where you enter abC, go back to the beginning, replace ab with AC, C with B, move to the next tab stop, then one more column to the right, then two columns to the left, then enter D.



You see:



ABC    D


That is, ABC, a 4 column gap and D.



If you select that with the mouse in xterm or putty, they will store in the selection ABC, 4 space characters and D, not abC<CR>AC<BS>B<Tab><Esc>[C<BS><BS>D.



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 <U+0065><U+0301> (e followed by a combining acute accent) changed to <U+00E9> (é the pre-composed form) by xterm.



Or echo abc that ends up being translated to ABC by the terminal driver before sending to the terminal after a stty olcuc.



Now, <Tab>, like <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 <CR> nor <LF> though).



For instance, in VTE-based terminals like gnome-terminal, you may see that, when you select the output of printf 'atbn' on an empty line, gnome-terminal actually stores atb in the X11 selection instead of a, 7 spaces and b.



But for the output of printf 'atbbn', it stores a, 6 spaces and b, and for printf 'artbn', a, 7 spaces and b.



There are other cases where the terminals will try to copy the actual input, like when you select two lines after running printf 'a nbn' 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 'footbarn' | xclip -sel c


Note that when you paste that in xterm or most other terminals, xterm actually replaces that n with r because that's the character xterm sends when you press Enter (and the terminal driver may translate it back to n).






share|improve this answer























  • This is very insightful, thank you. I have tried the xclip solution and it works. But it doesn't do exactly what I had in mind and requires X11. May be this will come in handy at some point, thanks!
    – Asu
    Dec 17 at 1:04










  • @Asu, X11 is what handles the copy-paste selection in terminal emulators like xterm or putty on Unix. Other terminal emulators may have they own copy-paste mechanism and ways to store arbitrary content in there, like the readbuf and register commands in GNU screen.
    – Stéphane Chazelas
    Dec 17 at 6:53











Your Answer








StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "106"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: false,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: null,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});






Asu is a new contributor. Be nice, and check out our Code of Conduct.










draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f489150%2fhow-to-print-strings-separated-by-tab-in-bash%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























3 Answers
3






active

oldest

votes








3 Answers
3






active

oldest

votes









active

oldest

votes






active

oldest

votes









9














Like ilkkachu said, this isn't an issue with bash, but with the terminal emulator which converts tabs to spaces on output.



Checking different terminals, putty, xterm, and konsole convert tabs to spaces, while urxvt and gnome-terminal do not. So, another solution is to switch terminals.






share|improve this answer



















  • 3




    It can also be done by the tty driver after you run stty tab3.
    – Stéphane Chazelas
    Dec 15 at 19:09
















9














Like ilkkachu said, this isn't an issue with bash, but with the terminal emulator which converts tabs to spaces on output.



Checking different terminals, putty, xterm, and konsole convert tabs to spaces, while urxvt and gnome-terminal do not. So, another solution is to switch terminals.






share|improve this answer



















  • 3




    It can also be done by the tty driver after you run stty tab3.
    – Stéphane Chazelas
    Dec 15 at 19:09














9












9








9






Like ilkkachu said, this isn't an issue with bash, but with the terminal emulator which converts tabs to spaces on output.



Checking different terminals, putty, xterm, and konsole convert tabs to spaces, while urxvt and gnome-terminal do not. So, another solution is to switch terminals.






share|improve this answer














Like ilkkachu said, this isn't an issue with bash, but with the terminal emulator which converts tabs to spaces on output.



Checking different terminals, putty, xterm, and konsole convert tabs to spaces, while urxvt and gnome-terminal do not. So, another solution is to switch terminals.







share|improve this answer














share|improve this answer



share|improve this answer








edited Dec 15 at 22:08









ilkkachu

55.3k782150




55.3k782150










answered Dec 15 at 17:57









JoL

987310




987310








  • 3




    It can also be done by the tty driver after you run stty tab3.
    – Stéphane Chazelas
    Dec 15 at 19:09














  • 3




    It can also be done by the tty driver after you run stty tab3.
    – Stéphane Chazelas
    Dec 15 at 19:09








3




3




It can also be done by the tty driver after you run stty tab3.
– Stéphane Chazelas
Dec 15 at 19:09




It can also be done by the tty driver after you run stty tab3.
– Stéphane Chazelas
Dec 15 at 19:09













14















the whitespace between the two is actually 5 spaces.




No, it's not. Not in the output of echo or printf.



$ echo -e 'footbar' | od -c
0000000 f o o t b a r n
0000010



What is the correct way to force tab being printed as tab, so I can select the output and copy it to somewhere else, with tabs?




This is a different issue. It's not about the shell but the terminal emulator, which converts the tabs to spaces on output. Many, but not all of them do that.



It may be easier to redirect the output with tabs to a file, and copy it from there, or to use unexpand on the output to convert spaces to tabs. (Though it also can't know what whitespace was tabs to begin with, and will convert all of it to tabs, if possible.) This of course would depend on what, exactly, you need to do with the output.






share|improve this answer























  • I meant that when I try to select the output, it is being treated as 5 spaces. Thanks for the 'od -c' to verify the contents of the command output.
    – Asu
    Dec 15 at 17:14






  • 1




    @Asu I think he understands that. His solution is to get the output via other means since the terminal emulator is not guaranteed to leave tabs as tabs when you select them in the window. However, I just checked and while putty, xterm, and konsole convert tabs to spaces, urxvt and gnome-terminal do not. So, another solution is to switch terminals.
    – JoL
    Dec 15 at 17:47












  • @JoL Yes, that's the conclusion I just came to a minute ago, and I think it would be the accepted answer if somebody cares to post it as such...
    – Asu
    Dec 15 at 17:50






  • 1




    @Asu, yeah, I thought about just working around the issue manually. It would be annoying to have to do that, but then I admit I hadn't realized that there are terminal emulators that do support copying tabs. Changing to one that does, would of course be a much better solution!
    – ilkkachu
    Dec 15 at 22:16
















14















the whitespace between the two is actually 5 spaces.




No, it's not. Not in the output of echo or printf.



$ echo -e 'footbar' | od -c
0000000 f o o t b a r n
0000010



What is the correct way to force tab being printed as tab, so I can select the output and copy it to somewhere else, with tabs?




This is a different issue. It's not about the shell but the terminal emulator, which converts the tabs to spaces on output. Many, but not all of them do that.



It may be easier to redirect the output with tabs to a file, and copy it from there, or to use unexpand on the output to convert spaces to tabs. (Though it also can't know what whitespace was tabs to begin with, and will convert all of it to tabs, if possible.) This of course would depend on what, exactly, you need to do with the output.






share|improve this answer























  • I meant that when I try to select the output, it is being treated as 5 spaces. Thanks for the 'od -c' to verify the contents of the command output.
    – Asu
    Dec 15 at 17:14






  • 1




    @Asu I think he understands that. His solution is to get the output via other means since the terminal emulator is not guaranteed to leave tabs as tabs when you select them in the window. However, I just checked and while putty, xterm, and konsole convert tabs to spaces, urxvt and gnome-terminal do not. So, another solution is to switch terminals.
    – JoL
    Dec 15 at 17:47












  • @JoL Yes, that's the conclusion I just came to a minute ago, and I think it would be the accepted answer if somebody cares to post it as such...
    – Asu
    Dec 15 at 17:50






  • 1




    @Asu, yeah, I thought about just working around the issue manually. It would be annoying to have to do that, but then I admit I hadn't realized that there are terminal emulators that do support copying tabs. Changing to one that does, would of course be a much better solution!
    – ilkkachu
    Dec 15 at 22:16














14












14








14







the whitespace between the two is actually 5 spaces.




No, it's not. Not in the output of echo or printf.



$ echo -e 'footbar' | od -c
0000000 f o o t b a r n
0000010



What is the correct way to force tab being printed as tab, so I can select the output and copy it to somewhere else, with tabs?




This is a different issue. It's not about the shell but the terminal emulator, which converts the tabs to spaces on output. Many, but not all of them do that.



It may be easier to redirect the output with tabs to a file, and copy it from there, or to use unexpand on the output to convert spaces to tabs. (Though it also can't know what whitespace was tabs to begin with, and will convert all of it to tabs, if possible.) This of course would depend on what, exactly, you need to do with the output.






share|improve this answer















the whitespace between the two is actually 5 spaces.




No, it's not. Not in the output of echo or printf.



$ echo -e 'footbar' | od -c
0000000 f o o t b a r n
0000010



What is the correct way to force tab being printed as tab, so I can select the output and copy it to somewhere else, with tabs?




This is a different issue. It's not about the shell but the terminal emulator, which converts the tabs to spaces on output. Many, but not all of them do that.



It may be easier to redirect the output with tabs to a file, and copy it from there, or to use unexpand on the output to convert spaces to tabs. (Though it also can't know what whitespace was tabs to begin with, and will convert all of it to tabs, if possible.) This of course would depend on what, exactly, you need to do with the output.







share|improve this answer














share|improve this answer



share|improve this answer








edited Dec 15 at 22:10

























answered Dec 15 at 16:29









ilkkachu

55.3k782150




55.3k782150












  • I meant that when I try to select the output, it is being treated as 5 spaces. Thanks for the 'od -c' to verify the contents of the command output.
    – Asu
    Dec 15 at 17:14






  • 1




    @Asu I think he understands that. His solution is to get the output via other means since the terminal emulator is not guaranteed to leave tabs as tabs when you select them in the window. However, I just checked and while putty, xterm, and konsole convert tabs to spaces, urxvt and gnome-terminal do not. So, another solution is to switch terminals.
    – JoL
    Dec 15 at 17:47












  • @JoL Yes, that's the conclusion I just came to a minute ago, and I think it would be the accepted answer if somebody cares to post it as such...
    – Asu
    Dec 15 at 17:50






  • 1




    @Asu, yeah, I thought about just working around the issue manually. It would be annoying to have to do that, but then I admit I hadn't realized that there are terminal emulators that do support copying tabs. Changing to one that does, would of course be a much better solution!
    – ilkkachu
    Dec 15 at 22:16


















  • I meant that when I try to select the output, it is being treated as 5 spaces. Thanks for the 'od -c' to verify the contents of the command output.
    – Asu
    Dec 15 at 17:14






  • 1




    @Asu I think he understands that. His solution is to get the output via other means since the terminal emulator is not guaranteed to leave tabs as tabs when you select them in the window. However, I just checked and while putty, xterm, and konsole convert tabs to spaces, urxvt and gnome-terminal do not. So, another solution is to switch terminals.
    – JoL
    Dec 15 at 17:47












  • @JoL Yes, that's the conclusion I just came to a minute ago, and I think it would be the accepted answer if somebody cares to post it as such...
    – Asu
    Dec 15 at 17:50






  • 1




    @Asu, yeah, I thought about just working around the issue manually. It would be annoying to have to do that, but then I admit I hadn't realized that there are terminal emulators that do support copying tabs. Changing to one that does, would of course be a much better solution!
    – ilkkachu
    Dec 15 at 22:16
















I meant that when I try to select the output, it is being treated as 5 spaces. Thanks for the 'od -c' to verify the contents of the command output.
– Asu
Dec 15 at 17:14




I meant that when I try to select the output, it is being treated as 5 spaces. Thanks for the 'od -c' to verify the contents of the command output.
– Asu
Dec 15 at 17:14




1




1




@Asu I think he understands that. His solution is to get the output via other means since the terminal emulator is not guaranteed to leave tabs as tabs when you select them in the window. However, I just checked and while putty, xterm, and konsole convert tabs to spaces, urxvt and gnome-terminal do not. So, another solution is to switch terminals.
– JoL
Dec 15 at 17:47






@Asu I think he understands that. His solution is to get the output via other means since the terminal emulator is not guaranteed to leave tabs as tabs when you select them in the window. However, I just checked and while putty, xterm, and konsole convert tabs to spaces, urxvt and gnome-terminal do not. So, another solution is to switch terminals.
– JoL
Dec 15 at 17:47














@JoL Yes, that's the conclusion I just came to a minute ago, and I think it would be the accepted answer if somebody cares to post it as such...
– Asu
Dec 15 at 17:50




@JoL Yes, that's the conclusion I just came to a minute ago, and I think it would be the accepted answer if somebody cares to post it as such...
– Asu
Dec 15 at 17:50




1




1




@Asu, yeah, I thought about just working around the issue manually. It would be annoying to have to do that, but then I admit I hadn't realized that there are terminal emulators that do support copying tabs. Changing to one that does, would of course be a much better solution!
– ilkkachu
Dec 15 at 22:16




@Asu, yeah, I thought about just working around the issue manually. It would be annoying to have to do that, but then I admit I hadn't realized that there are terminal emulators that do support copying tabs. Changing to one that does, would of course be a much better solution!
– ilkkachu
Dec 15 at 22:16











4














In printf '%st%sn' foo bar, printf does output foo<TAB>bar<LF>.



f, o, b, a and 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.



<Tab> and <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:



foo     bar


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:



xxfooyyybarz


On terminals that don't support tabulation, the terminal driver can be configured to translate those tabs to sequences of spaces. (stty tab3).



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: <Esc>, [, C.



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 vi, lynx, mutt, 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:



printf 'abCrACbBte[CbbDn'


Which simulates an editor session where you enter abC, go back to the beginning, replace ab with AC, C with B, move to the next tab stop, then one more column to the right, then two columns to the left, then enter D.



You see:



ABC    D


That is, ABC, a 4 column gap and D.



If you select that with the mouse in xterm or putty, they will store in the selection ABC, 4 space characters and D, not abC<CR>AC<BS>B<Tab><Esc>[C<BS><BS>D.



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 <U+0065><U+0301> (e followed by a combining acute accent) changed to <U+00E9> (é the pre-composed form) by xterm.



Or echo abc that ends up being translated to ABC by the terminal driver before sending to the terminal after a stty olcuc.



Now, <Tab>, like <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 <CR> nor <LF> though).



For instance, in VTE-based terminals like gnome-terminal, you may see that, when you select the output of printf 'atbn' on an empty line, gnome-terminal actually stores atb in the X11 selection instead of a, 7 spaces and b.



But for the output of printf 'atbbn', it stores a, 6 spaces and b, and for printf 'artbn', a, 7 spaces and b.



There are other cases where the terminals will try to copy the actual input, like when you select two lines after running printf 'a nbn' 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 'footbarn' | xclip -sel c


Note that when you paste that in xterm or most other terminals, xterm actually replaces that n with r because that's the character xterm sends when you press Enter (and the terminal driver may translate it back to n).






share|improve this answer























  • This is very insightful, thank you. I have tried the xclip solution and it works. But it doesn't do exactly what I had in mind and requires X11. May be this will come in handy at some point, thanks!
    – Asu
    Dec 17 at 1:04










  • @Asu, X11 is what handles the copy-paste selection in terminal emulators like xterm or putty on Unix. Other terminal emulators may have they own copy-paste mechanism and ways to store arbitrary content in there, like the readbuf and register commands in GNU screen.
    – Stéphane Chazelas
    Dec 17 at 6:53
















4














In printf '%st%sn' foo bar, printf does output foo<TAB>bar<LF>.



f, o, b, a and 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.



<Tab> and <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:



foo     bar


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:



xxfooyyybarz


On terminals that don't support tabulation, the terminal driver can be configured to translate those tabs to sequences of spaces. (stty tab3).



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: <Esc>, [, C.



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 vi, lynx, mutt, 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:



printf 'abCrACbBte[CbbDn'


Which simulates an editor session where you enter abC, go back to the beginning, replace ab with AC, C with B, move to the next tab stop, then one more column to the right, then two columns to the left, then enter D.



You see:



ABC    D


That is, ABC, a 4 column gap and D.



If you select that with the mouse in xterm or putty, they will store in the selection ABC, 4 space characters and D, not abC<CR>AC<BS>B<Tab><Esc>[C<BS><BS>D.



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 <U+0065><U+0301> (e followed by a combining acute accent) changed to <U+00E9> (é the pre-composed form) by xterm.



Or echo abc that ends up being translated to ABC by the terminal driver before sending to the terminal after a stty olcuc.



Now, <Tab>, like <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 <CR> nor <LF> though).



For instance, in VTE-based terminals like gnome-terminal, you may see that, when you select the output of printf 'atbn' on an empty line, gnome-terminal actually stores atb in the X11 selection instead of a, 7 spaces and b.



But for the output of printf 'atbbn', it stores a, 6 spaces and b, and for printf 'artbn', a, 7 spaces and b.



There are other cases where the terminals will try to copy the actual input, like when you select two lines after running printf 'a nbn' 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 'footbarn' | xclip -sel c


Note that when you paste that in xterm or most other terminals, xterm actually replaces that n with r because that's the character xterm sends when you press Enter (and the terminal driver may translate it back to n).






share|improve this answer























  • This is very insightful, thank you. I have tried the xclip solution and it works. But it doesn't do exactly what I had in mind and requires X11. May be this will come in handy at some point, thanks!
    – Asu
    Dec 17 at 1:04










  • @Asu, X11 is what handles the copy-paste selection in terminal emulators like xterm or putty on Unix. Other terminal emulators may have they own copy-paste mechanism and ways to store arbitrary content in there, like the readbuf and register commands in GNU screen.
    – Stéphane Chazelas
    Dec 17 at 6:53














4












4








4






In printf '%st%sn' foo bar, printf does output foo<TAB>bar<LF>.



f, o, b, a and 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.



<Tab> and <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:



foo     bar


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:



xxfooyyybarz


On terminals that don't support tabulation, the terminal driver can be configured to translate those tabs to sequences of spaces. (stty tab3).



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: <Esc>, [, C.



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 vi, lynx, mutt, 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:



printf 'abCrACbBte[CbbDn'


Which simulates an editor session where you enter abC, go back to the beginning, replace ab with AC, C with B, move to the next tab stop, then one more column to the right, then two columns to the left, then enter D.



You see:



ABC    D


That is, ABC, a 4 column gap and D.



If you select that with the mouse in xterm or putty, they will store in the selection ABC, 4 space characters and D, not abC<CR>AC<BS>B<Tab><Esc>[C<BS><BS>D.



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 <U+0065><U+0301> (e followed by a combining acute accent) changed to <U+00E9> (é the pre-composed form) by xterm.



Or echo abc that ends up being translated to ABC by the terminal driver before sending to the terminal after a stty olcuc.



Now, <Tab>, like <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 <CR> nor <LF> though).



For instance, in VTE-based terminals like gnome-terminal, you may see that, when you select the output of printf 'atbn' on an empty line, gnome-terminal actually stores atb in the X11 selection instead of a, 7 spaces and b.



But for the output of printf 'atbbn', it stores a, 6 spaces and b, and for printf 'artbn', a, 7 spaces and b.



There are other cases where the terminals will try to copy the actual input, like when you select two lines after running printf 'a nbn' 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 'footbarn' | xclip -sel c


Note that when you paste that in xterm or most other terminals, xterm actually replaces that n with r because that's the character xterm sends when you press Enter (and the terminal driver may translate it back to n).






share|improve this answer














In printf '%st%sn' foo bar, printf does output foo<TAB>bar<LF>.



f, o, b, a and 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.



<Tab> and <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:



foo     bar


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:



xxfooyyybarz


On terminals that don't support tabulation, the terminal driver can be configured to translate those tabs to sequences of spaces. (stty tab3).



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: <Esc>, [, C.



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 vi, lynx, mutt, 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:



printf 'abCrACbBte[CbbDn'


Which simulates an editor session where you enter abC, go back to the beginning, replace ab with AC, C with B, move to the next tab stop, then one more column to the right, then two columns to the left, then enter D.



You see:



ABC    D


That is, ABC, a 4 column gap and D.



If you select that with the mouse in xterm or putty, they will store in the selection ABC, 4 space characters and D, not abC<CR>AC<BS>B<Tab><Esc>[C<BS><BS>D.



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 <U+0065><U+0301> (e followed by a combining acute accent) changed to <U+00E9> (é the pre-composed form) by xterm.



Or echo abc that ends up being translated to ABC by the terminal driver before sending to the terminal after a stty olcuc.



Now, <Tab>, like <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 <CR> nor <LF> though).



For instance, in VTE-based terminals like gnome-terminal, you may see that, when you select the output of printf 'atbn' on an empty line, gnome-terminal actually stores atb in the X11 selection instead of a, 7 spaces and b.



But for the output of printf 'atbbn', it stores a, 6 spaces and b, and for printf 'artbn', a, 7 spaces and b.



There are other cases where the terminals will try to copy the actual input, like when you select two lines after running printf 'a nbn' 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 'footbarn' | xclip -sel c


Note that when you paste that in xterm or most other terminals, xterm actually replaces that n with r because that's the character xterm sends when you press Enter (and the terminal driver may translate it back to n).







share|improve this answer














share|improve this answer



share|improve this answer








edited Dec 16 at 18:30

























answered Dec 16 at 16:47









Stéphane Chazelas

298k54563910




298k54563910












  • This is very insightful, thank you. I have tried the xclip solution and it works. But it doesn't do exactly what I had in mind and requires X11. May be this will come in handy at some point, thanks!
    – Asu
    Dec 17 at 1:04










  • @Asu, X11 is what handles the copy-paste selection in terminal emulators like xterm or putty on Unix. Other terminal emulators may have they own copy-paste mechanism and ways to store arbitrary content in there, like the readbuf and register commands in GNU screen.
    – Stéphane Chazelas
    Dec 17 at 6:53


















  • This is very insightful, thank you. I have tried the xclip solution and it works. But it doesn't do exactly what I had in mind and requires X11. May be this will come in handy at some point, thanks!
    – Asu
    Dec 17 at 1:04










  • @Asu, X11 is what handles the copy-paste selection in terminal emulators like xterm or putty on Unix. Other terminal emulators may have they own copy-paste mechanism and ways to store arbitrary content in there, like the readbuf and register commands in GNU screen.
    – Stéphane Chazelas
    Dec 17 at 6:53
















This is very insightful, thank you. I have tried the xclip solution and it works. But it doesn't do exactly what I had in mind and requires X11. May be this will come in handy at some point, thanks!
– Asu
Dec 17 at 1:04




This is very insightful, thank you. I have tried the xclip solution and it works. But it doesn't do exactly what I had in mind and requires X11. May be this will come in handy at some point, thanks!
– Asu
Dec 17 at 1:04












@Asu, X11 is what handles the copy-paste selection in terminal emulators like xterm or putty on Unix. Other terminal emulators may have they own copy-paste mechanism and ways to store arbitrary content in there, like the readbuf and register commands in GNU screen.
– Stéphane Chazelas
Dec 17 at 6:53




@Asu, X11 is what handles the copy-paste selection in terminal emulators like xterm or putty on Unix. Other terminal emulators may have they own copy-paste mechanism and ways to store arbitrary content in there, like the readbuf and register commands in GNU screen.
– Stéphane Chazelas
Dec 17 at 6:53










Asu is a new contributor. Be nice, and check out our Code of Conduct.










draft saved

draft discarded


















Asu is a new contributor. Be nice, and check out our Code of Conduct.













Asu is a new contributor. Be nice, and check out our Code of Conduct.












Asu is a new contributor. Be nice, and check out our Code of Conduct.
















Thanks for contributing an answer to Unix & Linux Stack Exchange!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.





Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


Please pay close attention to the following guidance:


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f489150%2fhow-to-print-strings-separated-by-tab-in-bash%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

數位音樂下載

When can things happen in Etherscan, such as the picture below?

格利澤436b