If I run a command like:

watch "ps -ef | grep '^foo.*bar'"

and the matching output line of ps -ef is a long line like

foo <lots and lots of text here> bar

whether this line is output is affected by the terminal size. For example, if the terminal is full screen and this line fits on the screen, then the output will be shown in watch. However, if I resize the window to half the screen size so that the bar is truncated from the screen, then the grep line entirely disappears (presumably the line is truncated before it gets to grep in the pipeline). This is a problem because I'm trying to extract a portion of this line for use later in the pipeline.

Is there any solution to this to prevent the terminal size from affecting the output of grep within a watch command?


It's not grep, it's ps. Add the -ww options (ps -efww) to tell it to always output full lines.


I don't know what the right solutions is, but this seems to fix the problem,

watch "COLUMNS=160 ps -ef | grep '^foo.*bar'"

at the expense of having the lines from ps folded. Change "160" to be long enough for the longest "foo.*bar" line you expect to see.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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