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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T14:18:41+00:00 2026-06-09T14:18:41+00:00

I want to execute a python script from a bash script, and I want

  • 0

I want to execute a python script from a bash script, and I want to store the output of the python script in a variable.

In my python script, I print some stuff to screen and at the end I return a string with:

sys.exit(myString) 

In my bash script, I did the following:

outputString=`python myPythonScript arg1 arg2 arg3 `

But then when I check the value of outputString with echo $outputString I get everything that the Python script had printed to screen, but not the return value myString!

How should I do this?

EDIT: I need the string because that tells me where a file created by the Python script is located. I want to do something like:

fileLocation=`python myPythonScript1 arg1 arg2 arg1`
python myPythonScript2 $fileLocation
  • 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-09T14:18:43+00:00Added an answer on June 9, 2026 at 2:18 pm

    sys.exit(myString) doesn’t mean “return this string”. If you pass a string to sys.exit, sys.exit will consider that string to be an error message, and it will write that string to stderr. The closest concept to a return value for an entire program is its exit status, which must be an integer.

    If you want to capture output written to stderr, you can do something like

    python yourscript 2> return_file
    

    You could do something like that in your bash script

    output=$((your command here) 2> &1)
    

    This is not guaranteed to capture only the value passed to sys.exit, though. Anything else written to stderr will also be captured, which might include logging output or stack traces.

    example:

    test.py

    print "something"
    exit('ohoh') 
    

    t.sh

    va=$(python test.py 2>&1)                                                                                                                    
    mkdir $va
    

    bash t.sh

    edit

    Not sure why but in that case, I would write a main script and two other scripts… Mixing python and bash is pointless unless you really need to.

    import script1
    import script2
    
    if __name__ == '__main__':
        filename = script1.run(sys.args)
        script2.run(filename)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to execute python code from C# with following code. static void Main(string[]
I want to execute some Python code, typed at runtime, so I get the
I want to execute a code helloword.cpp which takes in some argument from console
While importing a python script from another script I want the script code that
I want to execute a Python CGI Script within a .shtml file, but I
I'm writing a python script that uses subprocess.Popen to execute two programs (from compiled
In Python I want to call a script that prints to the screen as
I used this line to execute a python script from a java application: Process
Possible Duplicate: Request UAC elevation from within a Python script? I'd like to execute
I want to execute assembly code inside a python script. Is that possible? In

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.