limon limon - 10 months ago 67
Bash Question

^H in bash variable

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".


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"
   `-- cursor position
$ echo "hello^H^H "
hel o
    `-- cursor position