Bash中的退格

Kyl*_*iss 4 bash backspace

你如何退回你刚刚用bash编写的那一行并在它的位置放一个新的?我知道这是可能的,Aptitude(apt-get)将它用于一些更新的东西,它看起来很棒.

Pau*_*ce. 11

试试这个:

$ printf "12345678\rABC\n"
ABC45678
Run Code Online (Sandbox Code Playgroud)

如您所见,输出回车会将光标移动到同一行的开头.

你可以像这样清除这条线:

$ printf "12345678\r$(tput el)ABC\n"
ABC
Run Code Online (Sandbox Code Playgroud)

使用tput为您提供了一种将控制字符发送到终端的便携方式.有关man 5 terminfo控制代码的列表,请参阅.通常,您需要将序列保存在变量中,这样您就不需要重复调​​用外部实用程序:

$ clear_eol=$(tput el)
$ printf "12345678\r${clear_eol}ABC\n"
ABC
Run Code Online (Sandbox Code Playgroud)