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.


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.

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

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

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