1

In the CentOS, I use:

[root@localhost sbin]# echo -e "ab\bc" ac 

the \b means backspace.

But when I use:

[root@localhost sbin]# echo -e "abc\b" abc 

It print out abc, why it do not backspace?

2 Answers 2

6

Backspace is not destructive by itself, though most editors (and even the command line) do make it seem that way. What really happens is that those programs, while interactive, make the cursor backup, they then write a space, and backup again to position the cursor correctly.

So your line could be echo -e "abc\b \b" and it'd work then.

You can test this by doing echo -n -e "abc\b" (the -n removes the automatic jump to the next line). Your prompt will now overwrite the 'c'.

This is somewhat similar to \n which means go to the next line (but not to the margin) and \r which is going to the margin (but not to the next line). Operating systems interpret those characters differently (Unix add a next-line to \n, DOS have both chars, etc).

6
  • There is no "white space" written by any editor. The backspace character just tells any program that process it to move backward one space. Try echo -e 'abcdef\b\b\bx' no backspace erase anything, only when the x gets printed, the d gets replaced. Commented Jun 19, 2017 at 18:33
  • What does this have to do with the original question? Commented Jun 19, 2017 at 23:04
  • The question is about the character \b, which is explicit in echo -e "ab\bc". Not about your description of the key Backspace. In fact, your answer has nothing to do with the question. Sorry. Commented Jun 19, 2017 at 23:07
  • Let us continue this discussion in chat. Commented Jun 19, 2017 at 23:33
  • @Arrow For input, the erase character (as in stty erase) can be ^H (BS) or ^? (DEL) depending on your religion. For output, it's ^H, not ^?. Commented Jun 20, 2017 at 21:56
3

Yes, this works as expected (the x character gets removed):

$ echo -e 'ax\bc' ac 

And yes, it "looks like" the backspace character gets lost with this:

$ echo -e 'abx\b' abx 

Note that the backspace character (\b) is not the same as the keyboard key Backspace.
The keyboard Backspace prints ^? with Ctrl-VBackspace.
That is interpreted by many editors as move back and remove a character. That is a lot more complex than just printing a white space, the whole line to the right needs to be moved one space (or two for wide characters, or zero for control characters) left for each char erased, or, if the erased character is a new line, move the entire line one line up. No, that is not a simple character, it is an entire procedure connected to the keyboard key Backspace.

However, no, the backspace character is actually written to the console.
You can look at what is being transmitted to the console with od:

$ echo -e 'abx\b' | od -vAn -tx1c 61 62 63 08 0a a b x \b \n 

So, what happens?

  1. The string abx gets written to the console.
  2. The console prints abx to the line buffer.
  3. The character \b gets written to the console.
  4. The character \b cause the console line buffer to move back one character.
  5. the character \n gets printed to the console.
  6. The console line buffer move to the next line (nothing is printed).
  7. A "end of string" gets printed to the console.
  8. The console outputs the whole line buffer.

That is the default way in which consoles work.

Take a look at this:

$ printf 'abcdef\b\b\b \n' abc ef 

The character d is the one replaced with a white space.

Why does the 'ax\bc' remove the x?

Because the c character overwrites the character x.

1
  • 1
    Whether the BackSpace key sends ^H or ^? depends on the terminal configuration. It has historically been a point of annoyance and flamewars, but ^? has largely won. This is not relevant here, though: the question is about terminal output, not about terminal input. Commented Jun 20, 2017 at 21:57

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.