Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

I want a similar behavior as less vim nano etc. uses.

Is this possible to achieve with bash?

share|improve this question
add comment

4 Answers 4

up vote 6 down vote accepted

If your terminal emulator supports it, you can do this using the same mechanism used by less, vim, et al, namely the ti and te termcap capabilities (smcup and rmcup in the more modern terminfo). These switch from the primary to the secondary screen, and back to the primary, respectively.

If your system has the tput command, you can use:

tput smcup # switch to secondary screen
...
tput rmcup # switch back to primary screen

Otherwise, for xterm, and probably for most other terminal emulators, this:

printf '\e[7\e[?47h'

should switch to the secondary screen, and this:

printf '\e[2J\e[?47l\e8'

should switch back to the primary (and put the cursor back where it was).

tcsh has a built-in echotc command, so you could do:

tcsh -c 'echotc ti'
...
tcsh -c 'echotc te'

(or just use echotc directly if you happen to be using tcsh).

share|improve this answer
    
tput smcup and tput rmcup seems to do the job. –  Tyilo Jan 1 '12 at 13:37
1  
If you’re going to hard-code the codes instead of using tput, try the modern 1049 instead of 47. This was introduced by Xterm and is supported by some other terminal emulators. It clears the alternate screen when switching to it, and leaves the contents in place when switching back (so you can view/copy the contents after the full-screen program has exited). –  Chris Page Jan 5 '12 at 9:01
1  
For robustness, if your scripting language supports it, arrange to switch to the main display even if your program exits prematurely. In a Bash script, for example, you could install a signal handler with trap 'tput rmcup' EXIT. –  Chris Page Jan 5 '12 at 9:05
add comment

None of the current answers seemed to work on my machine, but I found out about the command named screen, which seems to do the trick for me.

share|improve this answer
    
Not quite the same thing, but if it suits your purposes ... What terminal emulator are hou using? I think xterm has an option to (en|dis)able those control sequences; others likely do as well. –  Keith Thompson Dec 31 '11 at 3:53
    
I'm using iTerm 2 for OS X. –  Tyilo Dec 31 '11 at 3:55
add comment

This is called the “alternate screen” (and as the top google hits show, you are unusual in liking this feature). Some terminals support two screen buffers, one used by full-screen applications, the other used by line-oriented applications. Bash doesn't know anything about them. If a command simply emits output to the terminal, you can use terminal control sequences to switch to the secondary screen and back (these commands are documented in xterm ctlseqs).

printf '\e[?47h'; mycommand; printf '\e[?47l'

Note that there is no scrollback for the alternate screen, so you'll only see the last screenful of output. If that's a problem, just do the simple thing:

mycommand | less 

There are only two screen buffers. If you want more, use a proper screen multiplexer such as Screen or Tmux.

share|improve this answer
    
I'm surprised to see how disliked it is. I really like being able to run an editor and still see my command history when I'm done. –  Keith Thompson Dec 30 '11 at 8:51
    
There will be blank lines left after doing this in my term: cl.ly/Cz4N cl.ly/Cznp –  Tyilo Jan 1 '12 at 13:35
add comment

Bash knows nothing about screens (it can run without being seen, in fact). You can redirect both standard output and standard error to any file or pipe, though.

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.