18

How to format output of ps -p command?

To not show me something like this:

PID TTY                TIME CMD

but just PIDs.

I'm using Linux.

27

Use the -o option to select which columns are displayed. If you put = after the column name, the header line is suppressed.

ps -o pid= -p 1 23 456
ps -o pid= -o ppid= -o pgid= -o sid= -p 1 23 456
19

Use the --no-headers to have the header line omitted.

Original output:

ps
  PID TTY          TIME CMD
27027 pts/0    00:00:00 bash
27071 pts/0    00:00:00 ps

With --no-headers:

ps --no-headers
27027 pts/0    00:00:00 bash
27072 pts/0    00:00:00 ps

Combining with -p:

ps -p 1 --no-headers
    1 ?        00:00:33 systemd

The man page for ps clearly documents this:

man ps

[snip]

 --no-headers
          Print no header line at all.  --no-heading is an alias for this option.
  • 1
    Doesnt work on macos. Naturall -____- – Avindra Goolcharan Feb 10 '17 at 4:49
  • Indeed. Wasn't expected to work on MacOS. Question solely concerned Linux, as highlighted by the original poster. – steve Feb 10 '17 at 14:53
  • 1
    np steve. Just documenting it here for future readers. The minor differences between BSD and Linux utils is highly irritating. sed is the worst – Avindra Goolcharan Feb 11 '17 at 1:52
  • ok thx. if you've any thoughts on how to include MacOS solution in answer I'll happily revise it! – steve Feb 11 '17 at 9:46
  • 2
    i found that for particular column headers, putting a = after the column name to the o flag is a cross-platform way to do this. E.g., ps -p ${pid} -o state= will show the process state for ${pid}, without the header. (Which is what the accepted answer states :) ) – Avindra Goolcharan Feb 11 '17 at 16:17
2

Use

ps -p <PIDs> --no-headers

to get a list of PIDs without the header.

-1

To get a list of only the PIDs use:

ps -eo pid

See also man ps section 'STANDARD FORMAT SPECIFIERS' for possible colums.

Personally I like the way Solaris shows the possible columns to use for the -o argument:

ps: option requires an argument -- o
usage: ps [ -aAdefHlcjLPyZ ] [ -o format ] [ -t termlist ]
        [ -u userlist ] [ -U userlist ] [ -G grouplist ]
        [ -p proclist ] [ -g pgrplist ] [ -s sidlist ] [ -z zonelist ] [-h lgrplist]
  'format' is one or more of:
        user ruser group rgroup uid ruid gid rgid pid ppid pgid sid taskid ctid
        pri opri pcpu pmem vsz rss osz nice class time etime stime zone zoneid
        f s c lwp nlwp psr tty addr wchan fname comm args projid project pset lgrp

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.