在 Linux 终端中删除最后执行的命令

Rya*_*old 6 unix linux bash terminal console

我想做一个明确的,但只是我执行的最后一个命令。这是一个示例,以便您可以更好地理解它:

root@debian:~# id                               
uid=0(root) gid=0(root) groups=0(root)         
root@debian:~# uname -a 
Linux debian 3.2.0-4-amd64 #1 SMP Debian 3.2.54-2 x86_64 GNU/Linux
root@debian:~# 
Run Code Online (Sandbox Code Playgroud)

如果这是 shell 的当前状态,我想执行一个命令(例如echo a > /tmp/foo)并保留控制台:

root@debian:~# id                               
uid=0(root) gid=0(root) groups=0(root)         
root@debian:~# uname -a 
Linux debian 3.2.0-4-amd64 #1 SMP Debian 3.2.54-2 x86_64 GNU/Linux
root@debian:~# 
Run Code Online (Sandbox Code Playgroud)

所以它应该是这样的echo a > /tmp/foo && clear -n 1(我知道 clear 没有 -n 1 功能,这只是一个例子)。

谢谢

Eta*_*ner 2

为此,您需要在命令之前保存光标位置,然后在清除屏幕的其余部分的同时恢复该位置。

像这样的东西应该有效:

$ hiderun() {
    # Move cursor up one line.
    tput cuu 1
    # Save cursor position.
    tput sc
    # Execute the given command.
    "$@"
    # Restore the cursor position.
    tput rc
    # Clear to the end of the screen.
    tput ed
}

$ id
uid=0(root) gid=0(root) groups=0(root)
$ uname -a
Linux debian 3.2.0-4-amd64 #1 SMP Debian 3.2.54-2 x86_64 GNU/Linux
$ tput sc
$ hiderun do something
Run Code Online (Sandbox Code Playgroud)

这可能只适用于单行提示。多行提示可能需要将参数更改为tput cuu

嗯...让你的提示符tput sc作为第一件事运行可能意味着这可以正常工作,而不需要进行任何计数/等等。游戏,但这需要一些测试。