Output of ps | or > depends on terminal width

On my FreeBSD 10.2 and 10.3 systems, when I am using
Code:
ps aux | grep ...
the output depends on the terminal size. This seems seriously weird for me.

Isn't a pipe supposed to make command's output behave in a predictable way?

For example
Code:
ls
does all fancy formatting magic when run on its own, but when run like
Code:
ls | less
it always returns the same output styling.

But unlike
Code:
ls
, the output from
Code:
ps aux | less
cuts the lines to terminal width on FreeBSD (on bash at least). From my brief experimenting, the same command always outputs full length lines in Linux when used with pipes.

Why is this happening and how can I fix it?

Is this just a really weird FreeBSD default? From my understanding this "feature" would make it almost impossible to write reliable bash scripts on FreeBSD as the output of simple piped commands would not be reliable. Isn't this a problem?

Also, I've tried it with
Code:
ps aux > somefile
and the lines inside the file are cut exactly like how it was with grep and less!

Info: FreeBSD 10.2, bash 4.3.42. Same with connecting over SSH or when from KVM local console.
 
Yeah, for historical reasons ps(1) is doing the formatting based on terminal size regardless of what the output file is. There is the -w option that forces a 132 character line width for formatting but I'd use procstat(1) instead in scripts.
 
Yeah, for historical reasons ps(1) is doing the formatting based on terminal size regardless of what the output file is. There is the -w option that forces a 132 character line width for formatting but I'd use procstat(1) instead in scripts.

Thanks, but even 132 characters is an artificial limit, on linux the lines are as long as they need to be.

So is this just a particular ps behaviour, not general to other tools on BSD? I know about pgrep -f it does what I need to do, but I was seriously surprised how ps behaves so differently compared to on linux.
 
I think it's just ps(1) that behaves that way, I remembered/noticed now that you can specify the -w twice to remove any terminal size detection:

Code:
    -w      Use 132 columns to display information, instead of the default
             which is your window size.  If the -w option is specified more
             than once, ps will use as many columns as necessary without
             regard for your window size.  Note that this option has no effect
             if the “command” column is not the last column displayed.
 
Back
Top