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 3439788
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T08:21:12+00:00 2026-05-18T08:21:12+00:00

I am writing a bash script to redirect output from another command to the

  • 0

I am writing a bash script to redirect output from another command to the proper location. Basically, when the script is invoked from a shell/commandline I want to send the output to STDOUT. But, when the bash script is executed from some other application (e.g. another bash script, some application, or in my case from the awesome-prompt plugin in my Awesome Window Manager) I want to redirect the output somewhere else.

Is there any way in bash to see how a script was invoked?

  • 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-05-18T08:21:13+00:00Added an answer on May 18, 2026 at 8:21 am

    Try this:

    ps -o stat= -p $PPID
    

    If the result contains “s” (lowercase) it was either run from the command line or backgrounded from within a script. To tell those two apart:

    ps -o stat= -p $$
    

    will contain a “+” if it was not backgrounded.

    Here’s a table:

    Run          $$    $PPID
    CL           S+    Ss
    CL&          S     Ss+
    Script       S+    S+
    Script&      S     S
    Script(&)    S     Ss
    Script&(&)   S     NULL
    

    Where (&) means the child script was backgrounded and & means the parent script (which is what “Script” refers to) that ran it was backgrounded. CL means command line. NULL means that ps output a null and that $PPID is “1”.

    From man ps:

       s    is a session leader
       +    is in the foreground process group
    

    It should be noted that this answer is based on GNU ps, but the man pages for BSD (including OS X) indicate similar functionality. And GNU ps is a hybrid that includes BSD functionality, among others.

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

Sidebar

Related Questions

I am writing a shell (bash) script and I'm trying to figure out an
I am currently writing a bash script and when i run the useradd command
I am writing a bash shell script, run where the user is expected to
Writing a bash script, and I want to get user input. Awesome, read -p
I am writing a bash script to deal with some installations in an automated
I'm writing a bash script that needs to delete old files. It's currently implemented
I am writing a bash script to set up a production server. The tasks
I am writing a nightly build script in bash. Everything is fine and dandy
I'm writing a few little bash scripts under Ubuntu linux. I want to be
While writing a bash script to help creating polaroid thumbnail using Imagick's convert commmand.

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.