7

As I recall, ^H means Backspace. I generated it by pressing CTRL+V, CTRL+H.

So the ^H should remove the last character, as it is the same as Backspace and it should act just as if I pressed Backspace at this position.

Why the does the output of echo abc^H remain abc and not ab?

1

3 Answers 3

16

backspace is only moving the cursor backward.

backspace (or delete or whatever character depending on the configuration) deletes the last printed character only when these conditions are met:

  • it is typed on the keyboard, not a command output like echo in your example
  • the terminal device is in cooked mode (the usual case)

If you want to erase the c in your example, you need to overwrite it by another character, for example:

echo "abc^H "
3
  • Btw even echo "abc^H " didn't work for me, however echo -e "abc\b " did. I can't figure out why ^H doesn't work. ps. My shell is bash 4.1.5 .
    – rush
    May 3, 2012 at 7:48
  • 2
    @Rush: Make sure you enter ^H by typing Ctrl-V, Ctrl-H
    – jlliagre
    May 3, 2012 at 8:44
  • technically: if cooked mode (in the driver), or something that behaves like cooked mode such as bash+readline. Jun 4, 2019 at 1:12
0

By typing ^V^H, you are inserting a Backspace code in the string, not issuing a Backspace command. ^H is not required to be a destructive backspace; some software uses it for overprinting.

Afterthought: The creator(s) of echo(1) reasoned that if you type ^V and a control code, you do so because you want another process, not echo(1), to parse the control code. In the case of Ctrl-H, echo(1) puts 0x08 in the output string and shows that with a caret and an H. If the string is redirected to something that PARSES 0x08 as a destructive backspace in its INPUT, you might get the desired result.

2
  • The accepted answer already says so.
    – RalfFriedl
    Jun 2, 2019 at 17:20
  • echo(1) does none of these things; the terminal driver and/or shell does, then passes the result (a byte with value 0x08) to echo(1) which just outputs it like any other character code. Similarly in bash, ksh, zsh you can use $'\b' which the shell converts to an 0x08 and passes to the program or other command. Jun 4, 2019 at 1:13
-2

stty erase ^H that should solve it.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .