I run bash scripts. I want to run them both from terminal, and from GUI with "Run in Terminal" and leave after run interactive bash. Simply seeing output is explained here : add read -rn1.

Problem X:
I want a "clean" solution, to have at the end same terminal with bash that I can close with one exit if script is run from terminal and if from GUI. I can add bash -i, but if run from terminal it would require two exit to close the terminal. exec bash -i resulted in same.

Is there a way in a script to check if it was started from GUI via "Run in Terminal"?

Added 1 per comment:

ps aux | grep aaaa # while script started from GUI was running
mint       53293  0.1  0.0  11216  3356 pts/3    Ss+  21:58   0:00 /bin/bash /home/mint/aaaaa.sh

Noted the difference from one started from terminal is Ss+ instead of S+.

  • 1
    I suggest inspecting the output of env for extra environment variables that are being passed when run this way. Though I doubt that will yield a portable solution as DEs each tend to do their own thing. Commented Dec 12, 2021 at 10:04
  • 1
    Please start your bash script from the GUI, and while it is running start ps aux | less in another terminal window. Look through that list of processes, and find the line(s) that show the name of your script - or add a grep pipe onto the ps command above. Add that line(s) to your question.
    – Seamus
    Commented Dec 12, 2021 at 21:12
  • @Seamus, added, noted difference - see at the end. Commented Dec 12, 2021 at 22:06
  • OK - I may have an answer for you in a few... any feedback appreciated
    – Seamus
    Commented Dec 12, 2021 at 22:23

1 Answer 1


I assume your bash script is named /path/to/mybashscript.sh

Use ps to find mybashscript.sh ($0 if run within the script); include the state/stat columns to identify the specific states:

ps --sort +pid -eo pid,stat,command | grep "$0" | head -1 | awk '{print $2}' | grep "s"

or another way to filter out grep line:

ps -eo pid,stat,command | grep "$0" | grep -v grep | awk '{print $2}' | grep "s"

From your comments the difference is due to added s: s is a session leader for GUI way, Ubuntu does not have that way to start script from file manager to check.

From man ps, the state codes are as follows:

   Here are the different values that the s, stat and state output specifiers (header "STAT" or "S") will display
   to describe the state of a process:

           D    uninterruptible sleep (usually IO)
           I    Idle kernel thread
           R    running or runnable (on run queue)
           S    interruptible sleep (waiting for an event to complete)
           T    stopped by job control signal
           t    stopped by debugger during the tracing
           W    paging (not valid since the 2.6.xx kernel)
           X    dead (should never be seen)
           Z    defunct ("zombie") process, terminated but not reaped by its parent

   For BSD formats and when the stat keyword is used, additional characters may be displayed:

           <    high-priority (not nice to other users)
           N    low-priority (nice to other users)
           L    has pages locked into memory (for real-time and custom IO)
           s    is a session leader
           l    is multi-threaded (using CLONE_THREAD, like NPTL pthreads do)
           +    is in the foreground process group
  • thanks but looks like you have not understood correctly ;-) I want less manual typing each time, even extra exit better be eliminated, but you advice to run do so much ;-) I want to add lines to the script so that it handled all by itself. But due to your comment about ps aux I've found out output differs. So I hope I can use it within the script to do what I wanted. If you update the answer to use Ss vs S+ and hopefully explain what it means (cause I'd like to know as I'm curious), I think I'll gladly accept the answer. Commented Dec 12, 2021 at 23:03
  • @Martian2020: Glad to do that, but you'll have to explain what you're doing as I am still unclear on 2 things. First - is it Ss or S+ that you are looking for? Second - are you planning on using the PID obtained by ps to kill the chosen process (Ss or S+) in a script?
    – Seamus
    Commented Dec 12, 2021 at 23:28
  • as stated in the question: I use difference in output of ps within the script to find out how it was run. I doubt one can kill the script process and have both previous output and working bash after that in case of running from GUI. Commented Dec 12, 2021 at 23:31
  • to know what Ss+ means is needed e.g. to access portability of the solution. Already added to the script: ps aux | grep "$0" | grep 'Ss+' > /dev/null ; if [ $? -eq 0 ]; then bash -i; fi - not using basename is even better I think now. Commented Dec 12, 2021 at 23:34
  • man ps "s is a session leader". And "S interruptible sleep" so S was accidental, but using s might be workable, still more complex filtering is needed. Commented Dec 12, 2021 at 23:52

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .