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"
$ echo -e "word\b"

What's the reason behind this?

  • 1
    Take a look at this SO post: stackoverflow.com/q/5723935/6216002 – Haxiel Oct 7 '19 at 15:55
  • Thanks, that explains it – SantaXL Oct 7 '19 at 16:02
  • 1
    @SantaXL, please don't post screenshots of text to stack overflow. It would have been appropriate to copy the text from your terminal and paste into a text block on the site. – Daniel Farrell Oct 7 '19 at 16:13
  • @DanielFarrell ok, will remember this – SantaXL Oct 7 '19 at 17:36

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
  • 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 7 '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. – Daniel Farrell Oct 8 '19 at 3: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

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.