While working on a (Python) script which is of no consequence here, I have come across a very strange behavior involving watch
and ps aux
, when the latter is piped to something else.
I have managed to reduce the issue down to a single line. When running
watch "ps aux | grep 'ps aux'"
in a terminal, you usually get several lines of output, as is expected.
Note that the top three lines are cut off, and just barely fit ps aux
at the end. When you reduce the size of your terminal to where it does not fit any more, it is cut from the results completely.
This means that grep only receives the cut off output. What I find most baffling about this is the extremely limited scope for which this happens. It does not happen for either of
ps aux | grep "ps aux"
watch "ps u -C ps"
watch "ssh localhost 'ps aux | grep \"ps aux\"'"
In all of these cases, the list wraps around as expected.
This seems to be the case for both bash and sh, on Ubuntu 15.04.
While I managed to work around this issue in my script, does anyone have an explanation for this behavior?
Best Answer
Nasal demons.1
man ps
says (emphasis mine):Indeed, setting the
COLUMNS
variable manually helps:1Even though we're not talking about a C compiler...