Linux Terminal – View Command Output in Scrollable Pager

command linelesslinuxterminal

I would like to view the output of a command from the top and keep the terminal from jumping down, and I would like a terminal independent way for it. So without changing the terminal specific configuration. I tried using the less command like this:

command | less

however on the commands I used, the pager exits after command output has finished.

How do I get the behavior where the command output is streamed into the pager in real-time, but I can view it top to bottom?

Best Answer

Not exiting at the end of the input

By default, less does not exit automatically, only if you use the quit command (q).

If you invoke less with the option -E, it exits when it reaches the end of the file. With the option -e, it exits when you try to move past the end of the file (for example by scrolling repeatedly). With the option -F, less exits immediately if the file is entirely displayed on one screen. None of these options are enabled by default, but there are a few ways they can be made the default on your system:

  • less may be a shell alias with options. Check with alias less.
  • Less reads options from the environment variable LESS when it starts. Check with echo $LESS.
  • Less has a configuration file. Its default location is .less in your home directory; this can be changed by setting the environment variable LESSKEY. The file format is binary, but if options are set this way, both the string LESS and the options will appear in the file.
  • It's unlikely, but if the environment variable LESS_IS_MORE is set, less behaves like the older utility more, including exiting on end of file by default.

Viewing command output before it's finished

When you pipe the input of a command into less (mycommand | less), less keeps reading until the command has finished. You can interrupt less with Ctrl+C, but then you won't be able to read any further output from the command, and the command may die due to a broken pipe. This is because less closes the pipe when it stops reading.

A workaround for this is to make the command output to a file and call less on that file. When less is reading from a file, you can type F and then Ctrl+C to make it read more data from the file then continue browsing.

mycommand >log 2>&1 &
less log

Programs may have a slightly different behavior depending on whether their output is a terminal, a pipe or a file. If you find that output is delayed when the output is a file or a pipe as opposed to a terminal, that's due to output buffering. See Turn off buffering in pipe

The script utility runs a program in a virtual terminal and logs the whole output to a file. As far as the program is concerned, it's writing output to a terminal. The program may use color escape sequences, for example. Pass the -R option to less to make it pass terminal escape sequences to the terminal rather than show control characters in escaped form.

script -c 'mycommand --option || echo "mycommand failed with status $?"' mycommand.log </dev/null >/dev/null &
less -R mycommand.log
Related Question