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.
ps -ef | while read line; do
    for i in $line; do
        if [ $i = 'bash' ]; then
            echo $line;
        fi;
    done;
done;
unset i;

The command runs properly displaying the Bash processes but an additional Bash process is being created and displayed. I'm hoping it has something to do inside the if checking. I tried to run it with/without single/double quotes both for $i and bash inside the if condition. The process is still being created and shown. I could have used grep to get the output but wanted to try it this way.

Pls correct me.

share|improve this question
1  
What are you really trying to do? Most likely you want to use pgrep or a dedicated library instead. Manually parsing ps output is not what it's for, not least because of issues like this. –  Daniel Andersson Oct 3 '13 at 10:52

1 Answer 1

The extra process is created by the pipe.

For more information read this very good explanation of bash and the process tree.

Note that the output of ps is not designed to be parseable. When trying to parse ps you are most likely better of using pgrep.

share|improve this answer

Your Answer

 
discard

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.