You could probably do it by walking back up the ancestry of the shell and working out whether it was started by something that equates to "you", or another program.
Get the shell's PID (process ID), and from that its PPID (parent process ID). Keep going up until you get to something which tells you where it came from. You may need to experiment on your system -- at least, I don't know whether it'll be universal.
For example, on my system, get the PID of a shell and use ps
to show that it's bash
:
$ echo $$
18852
$ ps --pid 18852
PID TTY TIME CMD
18852 pts/1 00:00:00 bash
Get the PPID of 18852:
$ ps -o ppid= -p 18852
18842
Find out what the PPID (18842) is:
$ ps --pid 18842
PID TTY TIME CMD
18842 ? 00:00:02 gnome-terminal
We can see it's gnome-terminal, i.e. the terminal emulator / terminal window. Maybe that's good enough for you, if your shell launched by the other program is not running in a terminal emulator window.
If it's not good enough, go up another level:
$ ps -o ppid= -p 18842
2313
$ ps --pid 2313
PID TTY TIME CMD
2313 ? 00:00:00 init
This tells us that gnome-terminal
was started by init
. I suspect your shell started by another program will have something different there.