您应该使用备用屏幕终端功能.看到
Using the “alternate screen” in a bash script
“如何使用备用屏幕”的答案:
这个例子应该说明:
#!/bin/sh
: <
Shows the top of /etc/passwd on the terminal for 1 second
and then restores the terminal to exactly how it was
desc
tput smcup #save previous state
head -n$(tput lines) /etc/passwd #get a screenful of lines
sleep 1
tput rmcup #restore previous state
这只能在终端上工作,具有smcup和rmcup功能(例如,不在Linux控制台(=虚拟控制台)).
可以使用infocmp检查终端功能.
在不支持它的终端上,我的tput smcup只返回退出状态1而不输出转义序列.
注意:
如果您打算重定向输出,可能需要将转义序列直接写入/ dev / tty,以免弄乱它们的stdout:
exec 3>&1 #save old stdout
exec 1>/dev/tty #write directly to terminal by default
#...
cat /etc/passwd >&3 #write actual intended output to the original stdout
#...