limon limon - 6 months ago 18
Bash Question

^H in bash variable

#!/bin/bash
echo "hello^H^H" #hello
echo "hello^H^H " #hel o


I know ^H deletes back a letter in console. But why the first echo has no effect and second one gets "hel o".

Answer

The backspace just moves the cursor one position to the right without actually erasing what's printed on the screen. Only your second instruction actually erases the 4th character by overwriting it with the trailing space in your string.

I'll try to visualize it:

$ echo "hello^H^H"
hello
   ^
   `-- cursor position
$ echo "hello^H^H "
hel o
    ^
    `-- cursor position