1

I am trying to figure out why this happens in bash.

  • Ok this is easy enough.

    $ echo -e 'a\txy\bc'
    a   xc
    
  • Ok this is easy enough.

    $ echo -e 'a\txy\b\b\b\b\b\b\b\b\bc'
    ac      xy
    
  • Ok this is easy enough.

    $ echo -e 'a\txy\b\b\b\b\b\b\b\b\b\bc'
    c       xy
    
  • Now, why has c not dropped off the left end?

    $ echo -e 'a\txy\b\b\b\b\b\b\b\b\b\b\b\bc'
    c       xy
    

I expected the output to be:

<a tab>xy

But clearly that isn't the case. Anyone got a pointer as to what might be happening? Thanks.

2

This is nothing to do with the echo command. You'd see this same behaviour if you wrote the output using cat, printf, or some other program. This is an aspect of your terminal.

And terminals can differ amongst themselves in this regard. The terminfo database will or won't have, for your terminal, an auto_left_margin capability, known as bw in termcap. That tells programs whether backspace can be used to wrap around the left margin, as it can on some terminals. If you'd used a terminal with automatic left margins, the c would have appeared on the previous line.

And if you'd reprogrammed your tabstops, you'd have seen yet further different behaviour.

Interesting things can happen when one combines TAB and BS, by the way. The 25-year-old warning in the termcap manual about backspacing over the margin when there's no automatic left margin capability reported or when the cursor is on the first row, reprinted everywhere from the System V Interface Definition to the FreeBSD manual, may seem quaint and overcautious at first blush; but the world has known terminal control code processing that did not get this quite right.

Further reading

2

echo can't move back past the position it started at. No matter how many backspaces you use, once you've erased everything you've output it stays at the initial position. That's why you always see the 'c' character, however many times you backspace.

  • Thank you. I understand that @dr-jan. But why can it not move past the beginning of line? – gumchew Apr 24 '15 at 16:29
  • Probably because echo was written when the primary means of communicating with a computer was using a printing teletype. echo doesn't know about your screen, only the current line. – dr-jan Apr 24 '15 at 16:32
  • Heh. I'll dig into echo's code. Ta – gumchew Apr 24 '15 at 16:38
  • If you want to address your whole screen consider using ncurses or xterm ANSI escape codes to move around. – dr-jan Apr 24 '15 at 16:39
  • 3
    \b doesn't erase anything, it just writes the 0x8 character to stdout. When stdout is a terminal, that terminal understands that character as moving the cursor to the left by one column. For many terminals, that doesn't wrap, that is when the cursor is on the first column, it doesn't move back to the last column one line up. Some terminals do. Check for bw in the terminfo entry for those that do (like rxvt, Eterm...) – Stéphane Chazelas Apr 24 '15 at 16:45

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.