1

According to man:

If -e is in effect, the following sequences are recognized:

\b backspace

So I would expect echo -e "word\b" to produce wor output. But yet:

$ echo -e "wor\bd"
wod
$ echo -e "word\b"
word

What's the reason behind this?

SantaXL
  • 365

1 Answers1

1

Since this can't be marked a duplicate of an SO question, I guess it's valid to put an answer here in U&L.

\b moves the cursor back one character, but it doesn't erase that character. A final character printed would overwrite the character.

$ echo -e "hello world\bX"
hello worlX
erik258
  • 343
  • 1
  • 7
  • So I guess that behaviour of \b can be summed up to: replace the previous character with the next next one. But since there is no next one in word\b (it seems to ignore newline), leave it as it is. – SantaXL Oct 07 '19 at 18:40
  • 1
    could be wrong but I think the newline doesn't hit the terminal the same way as X in my example because the terminal's in cooked mode. You'll notice that if you type a command, scroll to the beginning or middle, and then hit enter, not just the part of the line left of the enter is sent; the whole command will be. \b is best summed up as 'move the character back one cursor' . It does not replace anything automatically, but the next output to the terminal that isn't some special control character, if any, will end up replacing it. – erik258 Oct 08 '19 at 03:10
  • Typing echo -ne "hello world\b" produces hello worl. echo -e "hello\n\bworld" prints hello <newline> world - it seems like \b does not work if the following char is a newline – SantaXL Oct 21 '19 at 20:45