Some shell commands that produce large outputs, such as git diff
, seem to cleverly throw up a pager when the output is too long, but not do so if it happens to be small.
How does this work? Does each individual program have its own code to measure its output, and invoke the pager? By the time it realizes the output is too long, wouldn't it have already printed some to stdout with no way to get it back? Or is this a generic shell feature where any program can be made to take advantage of it even if not explicitly programmed to do so?
To be clear: Of course foo | less
will force a pager and foo | cat
will force no pager. What I'm wondering is if there's a way to make any arbitrary foo
behave as foo | pager_if_needed
. I also don't mean literally implementing pager_if_needed
(probably can be done in 2-3 lines of bash) and doing alias foo="foo | pager_if_needed"
- since that would not also apply to bar
, baz
, and everything else.