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'
\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"
.
So to answer the actual question about backspaces this will simulate a backspace:
echo -e "\b \b"
It will move the character back one, then echo a space overwriting whatever character was there, then moving back again - in effect deleting the previous character. It won't go back up a line though so the output before that should not create a new line:
echo -n "blahh"; echo -e "\b \b"
It is not exactly what you're asking for, but also, in the line of Ignacio's answer, you could use for this case:
echo "$(ls | wc -l) items"
AFAIK you cannot print a character that deletes the one before, not even printing the char whose hexadecimanl number correspoonds to backspace. You can move back and print a blank space to delete, though. With cput you can do many things and print wherever you want in the screen.