Output tab character on terminal window

Tab is not a printable character. Tab is a control character that usually advances the cursor (but not at the end of line), leaving the characters that it's jumping through unchanged.

gnome-terminal (and other vte-based emulators) have a special hack that it tries to preserve tabs for copy-paste purposes, however, it still loses them at a soft linebreak. Other emulators might also have such a hack, but typically they don't.

See also the conversation at https://bugzilla.gnome.org/show_bug.cgi?id=769316.


If you use

printf "xx\t\tyy"

that should expand to a real tab-character. The behavior of echo in regard to bash has been erratic. I tested bash on my Debian 7, and found that neither echo -t nor /bin/echo -t gave a tab, while printf did (redirecting the output to a file to be sure). Here is the script:

#!/bin/bash
echo -t "xx\t\tyy"
echo done
/bin/echo -t "xx\t\tyy"
echo done
echo "xx\t\tyy"
echo done
/bin/echo "xx\t\tyy"
echo done
printf "xx\t\tyy"
echo done

and output to the terminal:

-t xx\t\tyy
done
-t xx\t\tyy
done
xx\t\tyy
done
xx\t\tyy
done
xx              yydone

That's distinct from the additional problem of putting characters on the terminal screen. As a rule, most terminals only have spaces selectable for cut/paste (irregardless of whether you print a tab or not). For that —

  • Is it possible to select tabs as tabs with mouse in urxvt?

A bit of a tanget, but if all you want is to get the output into your clipboard so you can paste that into Excel, you can try using something like xclip. Note that you might have to install it. With xclip I can write something like this.

echo -e "xx\t\tyy" | xclip -selection c

It is now in my clipboard I can paste it into Excel. If echo doesn't work you can try printf instead.

If you don't want to type all of that every time you can create an alias instead.

alias xclip="xclip -selection c"
echo -e "xx\t\tyy" | xclip

And if nothing else you can always redirect the output you want to a file and just open it in a text-editor and copy what you need as tabs should stay as tabs in the file.

echo -e "xx\t\tyy" > tmpfile.txt