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.
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.
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:
Location: $HOME/.config/Code/User/settings.json
Setting:
"terminal.integrated.profiles.linux": {
"bash": {
"path": "/bin/bash",
"icon": "terminal-bash",
"args": [
"-l"
]
}
}
Location: $HOME/Library/Application Support/Code/User/settings.json
Setting:
"terminal.integrated.profiles.osx": {
"bash": {
"path": "/bin/bash",
"icon": "terminal-bash",
"args": [
"-l"
]
}
}
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
.)
-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.terminal.integrated.shell.xxx
and terminal.integrated.shellargs.xxx
where xxx
is one of linux
, osx
, or windows
.~/.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).