bashshellvisual-studio-codesettings

VSCode Integrated Terminal Doesn't Load .bashrc or .bash_profile


I have the following files to handle shell configuration:

#~/.bash_profile
if [ -f ~/.bashrc ]; then
   source ~/.bashrc
fi

and

#~/.bashrc
... configure shell

If I open VSCode from the command line using code, my .bashrc is loaded whenever I add a new instance of the integrated shell.

However if I open VSCode via its icon, only my .profile is loaded.

How can I ensure my .bashrc is loaded instead?

I've tried various settings for the terminal.integrated.shellArgs.osx setting without any luck.


Solution

  • Why?

    The reason for the behavior is because .bashrc (indirectly, through /etc/profile) is only loaded for login shells, and the shell being launched is not a "login shell" nor has it inherited its environment/state by being launched from a login shell.

    How to Fix?

    The simplest way to correct the behavior (without changing any default system/profile configuration files) is to pass a -l (--login) option to bash instructing it to behave as a "login shell".

    In VSCode this can be done from user/global settings (ie. the settings.json file), the location of the user settings file and the config setting that needs to be modified varies by OS:

    Linux

    Location: $HOME/.config/Code/User/settings.json

    Setting:

        "terminal.integrated.profiles.linux": {
            "bash": {
                "path": "/bin/bash",
                "icon": "terminal-bash",
                "args": [
                    "-l"
                ]
            }
        }
    

    macOS

    Location: $HOME/Library/Application Support/Code/User/settings.json

    Setting:

        "terminal.integrated.profiles.osx": {
            "bash": {
                "path": "/bin/bash",
                "icon": "terminal-bash",
                "args": [
                    "-l"
                ]
            }
        }
    

    Windows

    Location: %USERPROFILE%\AppData\Roaming\Code\User\settings.json

    Setting:

        "terminal.integrated.profiles.windows": {
            "bash": {
                "path": "C:\\Windows\\system32\\bash.exe",
                "icon": "terminal-bash",
                "args": [
                    "-l"
                ]
            }
        }
    

    This will cause VSCode to launch bash as a login shell, executing the content of various runcom files (such as .bashrc.)

    Some Notes:

    1. If you exit and re-open VSCode with an active Terminal session, that session will be restored and relaunched using the args it was originally launched with (ie. it will not launch using the -l argument you have configured.) You will want to exit all active terminal sessions and start fresh terminal sessions to pick up your configuration change.
    2. If you use VSCode settings editor, there is a button at the top right to "open settings (JSON)". This is useful when your settings do not have any terminal profiles in it, because you can use tab-completion in VSCode to cause VSCode to emit a full config section reflecting your current terminal profiles config (there is a global default.) This may make it easier to customize.
    3. If an older installation of VSCode is in use, first remove the (now legacy) settings terminal.integrated.shell.xxx and terminal.integrated.shellargs.xxx where xxx is one of linux, osx, or windows.
    4. On a pedantic note, ".bashrc" is not a "profile" config file, it is a "runcom" file (aka "startup" file). The standard way to extend a shell profile (bash and a few others) is to modify "~/.profile" or "/etc/profile" config files, and not ".bashrc" as many have self-taught themselves to do. Using ~/.profile is a more-portable approach to shell profile management. Anything that is strictly a "bashism" should be kept in the bash-specific .bashrc file (meaning if you switch shells you can have a shared profile and not arrive at shell-specific brokenness), virtually every shell has its own runcom file(s).

    References: