Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8165723
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T19:37:42+00:00 2026-06-06T19:37:42+00:00

In a shell script lets say i have run a command like this for

  • 0

In a shell script lets say i have run a command like this

for i in `ps -ax|grep "myproj"`
do
  echo $i
done

Here, the grep command would be executed as a separate process. Then how do i get its PID in the shell script ?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-06T19:37:44+00:00Added an answer on June 6, 2026 at 7:37 pm

    I’m going out on a limb here, and understand this looks more like a comment.

    Why do you need the PID of the grep command?

    In your comment you say you want to compare it in the loop against something. I would suppose that it is your issue that that the loop will (sometimes) not only include myproj but also an item about your grep command? If so, try the following:

       for i in `ps -ax | grep -v grep | grep "myproj"`
       do
          echo $i
       done
    

    The -v switch basically inverts the pattern, so grep -v grep (or grep -v "grep", which maybe looks a bit less awkward) will include only lines that do not include the string “grep” (see man grep).
    Note that this maybe overly vague for some cases, for example if the pattern you actually look for also contains the string “grep”. For example, the following might not work as you’d expect: ps -ax | grep -v grep | grep mygrepling

    However, in your particular case, where you only look for “myproj” it will do.

    Or you could simply use

       for i in `ps -ax | grep "my[p]roj"`
       do
          echo $i
       done
    

    That way there is no need to know the PID of the grep command, because it simply never shows up as a loop iteration.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Let's say I've got a shell script called print_error.sh looking like this: #!/usr/bin/bash echo
Let's say I have a script like the following: useless.sh echo This Is Error
Let's say I do this in a unix shell $ some-script.sh | grep mytext
Lets say I have a shell / bash script named test.sh with: #!/bin/bash TESTVARIABLE=hellohelloheloo
I have a shell script like this. line=$@ # get the complete first line
I have a Bash shell script in which I would like to pause execution
Is there a way that lets me run a shell script, and display the
a shell script: VAR=(aa bb cc) for i in ${VAR[@]} do echo $i; done
I wrote a shell script that lets me grep through a directory tree and
I have a list like this (let's say it is memorized in summ.txt): s1

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.