42

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.

4 Answers 4

64

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
1
  • 1
    This answer has the benefit of being portable (supported by BSDs and specified by POSIX. Commented Sep 13, 2019 at 13:50
36

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.
6
  • 4
    Doesnt work on macos. Naturall -____- Commented Feb 10, 2017 at 4:49
  • Indeed. Wasn't expected to work on MacOS. Question solely concerned Linux, as highlighted by the original poster. Commented Feb 10, 2017 at 14:53
  • 3
    np steve. Just documenting it here for future readers. The minor differences between BSD and Linux utils is highly irritating. sed is the worst Commented Feb 11, 2017 at 1:52
  • ok thx. if you've any thoughts on how to include MacOS solution in answer I'll happily revise it! Commented Feb 11, 2017 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 :) ) Commented Feb 11, 2017 at 16:17
3

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
2
  • 1
    This does not address the issue of removing the header that is displayed. Commented Sep 13, 2019 at 12:44
  • That's how you interpret 'just display PID'. Some can interpret this of just selecting one column, or as you apparently interpret: just values. Commented Sep 13, 2019 at 14:16

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.