Can I check if the terminal was started by Visual Studio Code?

I have a script which runs whenever I open the terminal (I put the command in .bashrc). Now when I open Visual Studio Code the script is obviously also executed in the internal terminal therein. I don‘t want this to happen. Is there any way to detect whether a terminal instance was started by VSCode so I can prevent the script from executing in that case? (I use bash)

Sorry if this is a dumb question, I am still fairly new to Linux and bash.


VS Code sets the standard environment variable TERM_PROGRAM in the terminal's environment to indicate what launched it, so you can solve your problem using this without needing to parse the process tree:

if [[ "$TERM_PROGRAM" == "vscode" ]]; then
  exit 0
fi

# Rest of script...

Using this potentially duplicate answer: https://askubuntu.com/a/1012277/307523

rick@alien:~$ echo $$
25119
───────────────────────────────────────────────────────────────────────────────────────────
rick@alien:~$ pstree -aps $$
systemd,1 splash fastboot kaslr
  └─lightdm,1026
      └─lightdm,1294 --session-child 12 19
          └─upstart,1838 --user
              └─gnome-terminal-,25109
                  └─bash,25119
                      └─pstree,5696 -aps 25119

The environment variable $$ returns the current running processes PID (Process ID) which is the bash terminal.

The pstree command shows the entire "tree" of commands called.