Super User is a question and answer site for computer enthusiasts and power users. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I have a script that I want to have a different behaviour depending on whether it was launched from the terminal or by (double) clicking the icon in the file manager. Can I do this?

share|improve this question
Write a bah script that writes env to "scriptenv_$( date +%s )". Start once with each method, and compare the output. – Daniel Beck Nov 21 '11 at 9:44
up vote 4 down vote accepted

You can use differences in the return status from tty to help you.

if tty -s;
    # running in a terminal

(tty -s runs the tty command silently)

Exit status:

  • 0 if standard input is a terminal
  • 1 if standard input is not a terminal
  • 2 if given incorrect arguments
  • 3 if a write error occurs

Or you could use the shell's built-in tests to check whether standard input/output are from/to a terminal:

if [ -t 0 ];  # stdin
    # running in a terminal
share|improve this answer

If this is in your in your own computer, with you having make the icon, you could just pass an extra parameter on the command line referred by the icon, such as:

/path/to/my/script fromgui arg2 arg3 .. argN

And just test [[ "$1" == "fromgui" ]] or simil.

share|improve this answer

Another possible check, see what's in /proc/$PPID/cmdline

This is the command line (NUL delimited) of the process that spawned you,

share|improve this answer

You must log in to answer this question.

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