Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
465 views
in Technique[技术] by (71.8m points)

linux - Using the "alternate screen" in a bash script

The alternate screen is used by many "user-interactive" terminal applications like vim, htop, screen, alsamixer, less, ... It is like a different buffer of the terminal content, which disappears when the application exits, so the whole terminal gets restored and it looks like the application hasn't output anything.

I'd like to achieve exactly the same thing in my own shell (bash) script, except that it doesn't have to be that portable. I'd stick to linux only and xterm-based terminal emulators; but the solution should use something like tput if it's possible. However, I don't want to use some external scripting language (or even something like C).

Although I don't want to use C (as it should be a bash-script with as few dependencies as possible), I had a look into the source code of less. It seems to use terminfo as the database and looks up the "ti" terminal capability in its initialisation. When removing the line, it doesn't use the alternate sceen, so I assumed that I found the responsible code line.

However, I can't find such a capability in man terminfo. But maybe I'm on the wrong path finding a solution for this. Maybe terminfo / tput isn't my friend.

So (how) can I use the alternate screen in a bash script? Does somebody know a simple application in which source code I may find a hint? (C application or bash script or whatever...)

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

You can switch to the alternate screen using this command:

$ tput smcup

And back with:

$ tput rmcup

These commands just output the appropriate escape sequences for your terminal. If it is an XTERM they will be equivalent to the (more known but less elegant or portable):

$ echo -e "e[?1049h"

And:

$ echo -e "e[?1049l"

For more terminal control commands see man 5 terminfo.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

2.1m questions

2.1m answers

60 comments

57.0k users

...