Incorrect Terminal.app output for long lines with tabs

bugcommand lineterminal

When I run the following command in Terminal.app:

echo -e "a\tb\tc\td\te\tf\tg\tyo"

I should get the following output (using a 40-column window so that lines don't wrap here, and replacing tabs with spaces so that the correct amount of space is shown here):

a       b       c       d       e       
f       g       yo

But instead, I get this output:

a       b       c       d       e       y
o

As you can see, the f and g are missing.

After trying various strings, I think the problem is that a tab at the end of a line does not advance the output to the next line, but instead leaves the output cursor one character before the line end. For example (still assuming a 40-column terminal), 4 tabs puts the cursor at position 32 of line 1. Another tab should put the cursor at position 0 of line 2, but instead the cursor is at position 39 of line 1, leaving room for one more character to be printed—and even at that point, the cursor is not advanced to the next line, so another tab will put us back to position 39. Hence, an unlimited number of (single-character + tab) strings will be silently ignored.

I've experimented with a variety of:

  • shells (including bash, sh, and csh)
  • terminal widths (including 40 columns, 80, and many others)
  • terminal types (including vt100 and xterm-color)
  • terminal character encodings (including UTF-8 and ascii)
  • terminal settings (including Escape non-ASCII input)

Also, a friend ran similar commands on a Linux system with a different terminal, and did not encounter the same bug.

Anybody know how to fix this? I'd be much obliged, as this bug is making it might tricky to debug some home-made shell scripts running on tab-separated files.

Best Answer

This has to do with line breaks. Terminal is looking for either a space, or a continuos string of characters to make a line break. In this case the first opportunity it sees to break the line is between the y and the o. The "y" is shown because the last character in an extended line is shown in the last place to hint the user that something is happening there.

Workarounds...

You could try:

echo -e "a \tb \tc \td \te \tf \tg \tyo"

and everything will appear as you'd expect it.

also something like

echo -e "ab\tcd\tef\tgh\tij\tkl\tmn\tyo"

should break between the "k" and "l"

Still this is odd behavior and defiantly worth a bug/radar report