How to get rid of the headers in a ps command in Mac OS X ? How to get rid of the headers in a ps command in Mac OS X ? bash bash

How to get rid of the headers in a ps command in Mac OS X ?


The BSD (and more generally POSIX) equivalent of GNU's ps --no-headers is a bit annoying, but, from the man page:

 -o      Display information associated with the space or comma sepa-         rated list of keywords specified.  Multiple keywords may also         be given in the form of more than one -o option.  Keywords may         be appended with an equals (`=') sign and a string.  This         causes the printed header to use the specified string instead         of the standard header.  If all keywords have empty header         texts, no header line is written.

So:

ps -p 747 -o '%cpu=,%mem='

That's it.

If you ever do need the remove the first line from an arbitrary command, tail makes that easy:

ps -p 747 -o '%cpu,%mem' | tail +2

Or, if you want to be completely portable:

ps -p 747 -o '%cpu,%mem' | tail -n +2

The cut command is sort of the column-based equivalent of the simpler row-based commands head and tail. (If you really do want to cut columns, it works… but in this case, you probably don't; it's much simpler to pass the -o params you want to ps in the first place, than to pass extras and try to snip them out.)

Meanwhile, I'm not sure why you think you need to eval something as the argument to echo, when that has the same effect as running it directly, and just makes things more complicated. For example, the following two lines are equivalent:

echo "$(ps -p 747 -o %cpu,%mem)" | cut -c 1-5ps -p 747 -o %cpu,%mem | cut -c 1-5


Using awk:

ps -p 747 -o %cpu,%mem | awk 'NR>1'

Using sed:

ps -p 747 -o %cpu,%mem | sed 1d


Use ps --no-headers:

--no-headers print no header line at all

or use:

ps | tail -n +2