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.


2 Answers 2


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

# Rest of script...

The one exception to this rule is if an extension creates a terminal and specifies the seldom used strictEnv flag. These are shells that the extension indicated it wants complete control over the environment.

  • Vscode didn't do this automatically for me, so I went to the terminal.integrated.env.linux option in the Vscode settings and added this environment variable manually.
    – Liron Lavi
    Mar 24, 2020 at 19:55
  • Possibly things have changed since your answer, but as of this writing, TERM_PROGRAM is actually not set during environment resolution, which happens when Code is started from the UI. See this question for details. Oct 15, 2021 at 2:54
  • It should be, sounds like you might be experiencing a bug? Please create an issue here github.com/Microsoft/vscode/issues/new Oct 16, 2021 at 13:58
  • In my case $TERM_PROGRAM wasn't set, but $TERM was set to dumb, so I used that to detect it instead. Sep 6, 2022 at 18:38

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

rick@alien:~$ echo $$
rick@alien:~$ pstree -aps $$
systemd,1 splash fastboot kaslr
      └─lightdm,1294 --session-child 12 19
          └─upstart,1838 --user
                      └─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.

  • 3
    To extend this: if pstree -s $$ | grep -Eq '\<code\>'; then echo started from code; fi Apr 3, 2018 at 11:37
  • 2
    We can omit if/then/fi: pstree -s $$ | grep -wq 'code' && echo true :)
    – pa4080
    Apr 3, 2018 at 19:55

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.