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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T06:07:07+00:00 2026-06-18T06:07:07+00:00

I want to do the following: If the bash/python script is launched from a

  • 0

I want to do the following:

If the bash/python script is launched from a terminal, it shall do something such as printing an error message text. If the script is launched from GUI session like double-clicking from a file browser, it shall do something else, e.g. display a GUI message box.

  • 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-18T06:07:08+00:00Added an answer on June 18, 2026 at 6:07 am

    You can check to see whether stdin and stdout are connected to a terminal or not. When run from a GUI, generally stdin is not connected at all, and stdout is connected to a log file. When run from a terminal, both stdin and stdout will be connected to a terminal.

    In Python:

    import os
    import sys
    
    if os.isatty(sys.stdout.fileno()):
        # print error message text
    else:
        # display GUI message
    

    You should check that this will work for you, though, since it doesn’t do precisely what you asked for. But it’s the best thing that I can think of that doesn’t depend on too much magic.

    You should check that the DISPLAY environment variable is set before going with GUI code too, since it won’t work without that.

    Note that terminal users can still redirect stdin or stdout to /dev/null (for example) and this might cause your program to go with the GUI behaviour. So it’s far from perfect.

    Finally, even though I’ve given you an answer, please don’t do this! It is confusing to users for a program’s behaviour to change depending on how it was called.

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

Sidebar

Related Questions

I want to execute a python script from a bash script, and I want
I want to add a variable ${date} in the following bash script: ffmpeg -i
I am trying to pass several parameters from a python script to a bash
I am returning strings from a python module, for consumption within a bash script.
hi I have following propertie file (something.properties) SERVER1_PROPERTY1=123 SERVER1_PROPERTY2=${SERVER1_PROPERTY1}/123 and following bash script fetching
I want a zsh/bash script to automate the following 2 steps command. $ tar
I want to print numbers in a bash script using the following C format
I want to run the following bash command in Python 3: ls -l I
i want the following functionality. input : this is test <b> bold text </b>
In Python I want to call a script that prints to the screen as

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.