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.

This question already has an answer here:

I often pipe commands to less in order to read through the output (e.g. compiler errors).

MyCommand | less

This is great because it makes trawling through large amounts of output easy, but when I exit less the output is gone. How can I make the output still visible after quitting less?

This question differs from Is there a way in bash to redirect output and still have it go to stdout? because that question relates to output to a text file via tee, which, as far as I know, doesn't provide a facility to split output between less and stdout.

share|improve this question

marked as duplicate by Braiam, Bernhard, derobert, chaos, garethTheRed Aug 13 at 20:57

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

    
@jasonwryan the question is similar but the accepted answer appears limited to redirecting output to files, at least that's my understanding of the tee command. I am not sure what the rules are regarding this situation but I don't think marking this as a duplicate would benefit the community unless the answer to that question is reviewed. –  quant Aug 12 at 23:19
    
@jasonwryan: I agree; they're not the same question. The other one is a shell question; Arman's question is really about less; namely, how to get it to leave the displayed text in the console's scrollable display history buffer. ... P.S. Arman: You might want to edit your question and retag is as a /less question. –  G-Man Aug 12 at 23:24
    
@Arman I edited your title & question some more, please feel free to revert my edit if I've misunderstood what you're asking. –  derobert Aug 13 at 3:48
    
@derobert that seems fine. –  quant Aug 13 at 5:18
2  
They aren't duplicates, they are about totally different things: he wants info about the problematic ncurses behavior of the less command, and this hasn't anything to do with the fd redirections! Shame to you! –  Peter Horvath Aug 13 at 8:31

4 Answers 4

up vote 12 down vote accepted

Using less -X:

Disables sending the termcap initialization and deinitialization strings to the terminal.

That will leave any text on-screen behind before and after paging. So:

command | less -X

will have the effect you want. Note that this output will still be wrong (duplicated lines) if you ever scrolled up - that's unavoidable without without writing to a file.

You can also set the environment variable LESS to a value that contains X to do this by default for every invocation of less.

If you want to write to a file without resorting to tee, you can use the less -o filename or --log-file=filename options.

share|improve this answer

You can use:

 command | tee filename.out | less

and then examine filename.out via an editor if you still need to see the output after running the less command.

Another idea is to change your term setting to use a dumb terminal type. You'd have to experiment with this one.

share|improve this answer

If the issue is just that less is "clearing the screen" when you quit it you can avoid that behavior specifically by telling less not to send the terminal/termcap init/de-init strings when it starts and stops. You do this by using -X. So try command | less -X.

That may have other side-effects if those strings do things that are actually necessary but I don't expect that to be the case for you here.

share|improve this answer

You can also try the command more in place of less. It will still paginate the output and allow you to scroll through it nicely, but in the end your terminal will retain the output as if it were not piped through anything.

share|improve this answer
    
Since less is more powerful than more, suggesting more is only moderately useful. For instance, with less you can scroll forth and back through the text even it is the output from a pipe - more is not capable of doing that that. –  countermode Aug 13 at 14:38
    
I suppose. I suggested it because it fills what the question desired, and no other functionality was mentioned. –  Carl Aug 13 at 19:42

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