How to check if script was executed via command line or double-clicked?

You can check if the parent process is the shell. For example:

#! /bin/bash

if [[ $(readlink -f /proc/$(ps -o ppid:1= -p $$)/exe) != $(readlink -f "$SHELL") ]]
then 
    echo "Starting the shell..."
    exec "$SHELL"
else
    echo "Not starting a shell."
fi

ps -o ppid:1= -p $$ prints the PID of the parent process (ppid) of the current process (-p $$). A readlink on /proc/<pid>/exe should print the path to the executable, which would be the shell if you ran it in a shell, or something else otherwise.


Another possibility is the SHLVL variable, which indicates how nested the current the shell instance is. If run within a shell, the script should have SHLVL 2 or greater. When run by double clicking, or from a desktop launcher, it should be 1:

#! /bin/bash

if (( SHLVL > 1 ))
then 
    echo "Starting the shell..."
    exec "$SHELL"
else
    echo "Not starting a shell."
fi