Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

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

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

in this case it will be: 11545

I have tried

PROCESS=$(ps ax | grep 'bash script_c.sh' | 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 stackoverflow.com 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

2 Answers 2

up vote 2 down vote accepted

The classic tool for extracting columns is cut.

pgrep -fl 'bash script_c.sh' | 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 script_c.sh'

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

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

pkill -f "script_c.sh"

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 script_c.sh | 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/ && /script_c.sh/ { 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]cript_c.sh 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 script_c.sh' | 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.