Echoing a backspace

sidyll picture sidyll · Apr 20, 2011 · Viewed 33k times · Source

Is it possible to echo a backspace in bash?

Something like

echo $'stack\b'

Shouldn't output stac? Or I'm missing something?

More specifically, I'd like to use that in:

ls | wc -l; echo $'\b items'

Answer

Ignacio Vazquez-Abrams picture Ignacio Vazquez-Abrams · Apr 20, 2011

\b makes the cursor move left, but it does not erase the character. Output a space if you want to erase it.

For some distributions you may also need to use -e switch of echo:

  -e     enable interpretation of backslash escapes

So it will look like

 echo -e 'stack\b '

Also, files=(*) ; echo "${#files[@]} items".