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

  • Home
  • SEARCH
  • 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'm writing a Bash script, and I want to checkout a tag and then
I am writing a bash script to copy a file from a remote server,
I'm writing a bash shell script that uses a case with three options: If
I'm writing a Bash script that sets up a Drupal development environment for people
I am writing a bash script that needs to print the date of the
I'm writing a BASH script to integrate our company's custom intranet with our postfix
I am writing a script that installs java on a remote machine. After i
I'm trying to write a bash script which will, given the name of a
I have no idea how to write Bash and after googling around for a
I bought a NAS box which has a cut down version of debian on

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.