bash 回声退格

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5723935/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 20:26:03  来源:igfitidea点击:

Echoing a backspace

bashecho

提问by sidyll

Is it possible to echo a backspace in bash?

是否可以在 bash 中回显退格?

Something like

就像是

echo $'stack\b'

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

不应该输出stac吗?或者我错过了什么?

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

更具体地说,我想将其用于:

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

回答by Ignacio Vazquez-Abrams

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

\b使光标向左移动,但不会擦除字符。如果要删除它,请输出一个空格。

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

对于某些发行版,您可能还需要使用以下-e开关echo

  -e     enable interpretation of backslash escapes
  -e     enable interpretation of backslash escapes

So it will look like

所以它看起来像

 echo -e 'stack\b '

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

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

回答by rawbone

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"

回答by Robert Vila

It is not exactly what you're asking for, but also, in the line of Ignacio's answer, you could use for this case:

这不完全是您所要求的,而且,在 Ignacio 的回答中,您可以在这种情况下使用:

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 cputyou can do many things and print wherever you want in the screen.

AFAIK 你不能打印一个删除之前的字符,甚至不能打印十六进制数字对应于退格的字符。不过,您可以向后移动并打印一个空白区域进行删除。使用cput,您可以做很多事情并在屏幕上的任何位置打印。