Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I need to extract only bash process' number, from this output:

11545 pts/3    S+     0:00 bash
11625 pts/3    S+     0:00 grep

in this case it will be: 11545

I have tried

PROCESS=$(ps ax | grep 'bash' | cut -d' ' -f1 | tr -d ' ' | sed '/^$/d')

After that I have to kill it using kill $PROCESS, but it doesn't work well, it says: "must be a pid of a job etc...".

share|improve this question

migrated from Feb 3 '13 at 16:34

This question came from our site for professional and enthusiast programmers.

It is also the first column. – iMom0 Feb 3 '13 at 11:42
Have you tried looking at the result of echo $PROCESS? – Anony-Mousse Feb 3 '13 at 11:52
up vote 2 down vote accepted

The classic tool for extracting columns is cut.

pgrep -fl 'bash' | cut -f1 -d" "

will extract the first column, separated by single space characters. It should actually do what you wanted. The bigger trick however was that I used pgrep, which has a much nicer output. If I hadn't added the -l switch, there would have been no need to cut:

pgrep -f 'bash'

However, for your particular task, you may want to just use pkill.

It allows killing processes by a pattern, e.g.

pkill -f ""

The big benefit is that pgrep and pkill are aware of themselves, and will only output/kill matching other processes. In your above example, you will have false positives such as the grep command for example. So just use pkill which should be available on any modern Linux/BSD system.

If you really want to use ps (which I do not recommend):

 ps ax | grep | grep -v grep | sed -e 's/^ *\([0-9]*\) .*/\1/'

or even better with awk (which is better at automatically parsing the column layout of ps):

  ps ax | awk '!/awk/ && / { print $1 }'

Note that for both you have to make sure you don't match yourself, i.e. exclude grep and awk from matching. So using pgrep is substantially simpler. Both pgrep and pkill are the right tools for your actual problem.

share|improve this answer
pkill works like a charm and cut is perfect to extract first column thanks – Frank Feb 3 '13 at 12:11
+1 You learn something every day. – aefxx Feb 3 '13 at 12:51
One additional handy script is to do |grep '[s] which will match the process but not match the grep. – Alan Shutko Feb 3 '13 at 16:43
@AlanShutko cute hack, but using pgrep obviously is much nicer. :-) – Anony-Mousse Feb 3 '13 at 20:02
It is, but it's not everywhere. – Alan Shutko Feb 3 '13 at 20:38

Try this:

PROCESS=$(ps ax | grep 'bash' | sed 's/\([0-9]*\).*/\1/')
share|improve this answer
thanks it works to extract that number – Frank Feb 3 '13 at 12:12

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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