VSCode 集成终端无法加载 .bashrc 或 .bash_profile

问题描述 投票:0回答:10

我有以下文件来处理 shell 配置:

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

#~/.bashrc
... configure shell

如果我使用

code
从命令行打开 VSCode,则每当我添加集成 shell 的新实例时,都会加载我的
.bashrc

但是,如果我通过 VSCode 的图标打开它,则只会加载我的

.profile

如何确保我的

.bashrc
已加载?

我尝试了

terminal.integrated.shellArgs.osx
设置的各种设置,但没有任何运气。

bash shell visual-studio-code settings
10个回答
51
投票

只需将 shell 参数添加到 VsCode

settings.json
文件即可。

settings.json
文件的路径如下:

Windows: C:\Users\<username>\AppData\Roaming\Code\User\settings.json`

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

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

添加以下内容之一:

"terminal.integrated.shellArgs.windows": ["-l"],

"terminal.integrated.shellArgs.linux": ["-l"],

"terminal.integrated.shellArgs.osx": ["-l"],

这将使用登录参数启动您选择的 shell。因此,这将执行设置的任何用户配置文件。


51
投票

VSCode 已弃用

"terminal.integrated.shellArgs.osx"
,转而使用配置文件。这对 osx 中的 bash 起到了作用。如果您不希望 bash 成为默认配置文件,请省略第一行:

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

28
投票

另一个对我有用的可能解决方案。 settings.json 文件(您可以在“文件”>“首选项”>“设置”>“功能”>“终端”>“集成”>“自动化 Shell:Linux”中访问)有一个参数

    "terminal.integrated.inheritEnv": false

默认设置为 false。将其更改为 true 解决了我的问题。


11
投票

我在 Mac 上的 Intellij Idea 终端上遇到了同样的问题,两者的解决方案是相同的。在设置中将集成终端的路径更改为“/bin/bash”。希望有帮助。


8
投票

您还可以尝试以下方法:

1 创建一个名为 /usr/local/bin/bash-login 的文件并添加:

#!/bin/bash
bash -l

2 运行:

chmod +x /usr/local/bin/bash-login 

使其可执行。

3 在您的 VSC 用户设置中添加

   { "terminal.integrated.shell.osx": "/usr/local/bin/bash-login" }

该解决方案已在 https://github.com/Microsoft/vscode/issues/7263 进行了描述。

希望有帮助


6
投票

我也遇到这个问题了。但就我而言,我在 Windows 上使用 Visual Studio 代码在我的 CentOS WSL 中打开远程开发环境。

因此修复此用例的配置有点不同。 在IDE中打开设置。然后在顶部选择“远程 [WSL: XXX]”

向下滚动到集成 -> 配置文件:Linux 然后点击settings.json中的编辑

然后我将以下内容添加到文件中:

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

保存设置文件,您打开的下一个终端将尊重您的 ~/.bash_profile。 注意:我的 ~/.bash_profile 具有其他人建议添加的相同行以加载其中的 bashrc 文件。


2
投票

"terminal.integrated.shellArgs.osx": ["-l"]
已弃用。

我个人想使用

.bash_profile
,所以我用这个做了一个
.bashrc

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

然后我必须执行完全重新启动计算机(仅重新启动 VS code 不起作用)。


2
投票

Bash 将依次加载各种 DotFiles:

  1. ~/.bash_profile
  2. ~/.bash_login
  3. ~/.profile

如果

~/.bash_profile
已加载,第二个和第三个将不会加载。

如果

~/.bash_profile
未加载,bash 将找到第二个。如果
~/.bash_login
已加载,第三个将不会加载。

如果

~/.bash_profile
~/.bash_login
都未加载,Bash 将尝试加载
~/.profile
文件。

全新的 Ubuntu 安装将仅包含

~/.profile
文件。所以我认为最好不要使用
~/.bash_profile
以避免问题发生。只需使用
~/.profile
文件即可。那么你的 VSCode 就不需要配置任何东西了。


2
投票

我看到很多人向 bash 推荐

-l
参数,但这对我不起作用。请改用
-i
参数(交互式)。这位于您的用户设置文件中。这是针对 Mac 操作系统的。如果在不同平台上,请将
osx
更改为
linux
windows

  "terminal.integrated.profiles.osx": {
        "bash": {
          "path": "bash",
          "args": ["-i"]
        }
    }

0
投票

bash -l
不加载 ~/.bashrc 文件,而
bash -i
则加载。原因是
bash -l
启动登录 shell,而
bash -i
启动交互式 shell。

当 bash 作为登录 shell 启动时,它从 /etc/profile 文件读取并执行命令,然后是以下存在且可读的第一个文件:~/.bash_profile、~/.bash_login 和 ~ /。轮廓。但是,当 bash 作为交互式 shell 启动时,它会从 ~/.bashrc 文件读取并执行命令。

因此,当您使用

bash -l
启动登录 shell 时,它不会读取 ~/.bashrc 文件。但是,当您使用
bash -i
启动交互式 shell 时,它会从 ~/.bashrc 文件读取并执行命令。

© www.soinside.com 2019 - 2024. All rights reserved.