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

73

我需要处理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_profile而不是bashrc。删除bash_profile文件解决了这个问题。 - madprops
10个回答

52

VSCode已经不推荐使用"terminal.integrated.shellArgs.osx",而是建议使用配置文件。对于在osx中的bash终端来说,可以这样做。如果你不想将bash设置为默认配置,可以省略第一行:

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

供其他人参考,对我来说很好用,无需在GUI中的Preference -> Settings中更改任何终端相关选项。我的VS Code版本为1.74.2 - Richard
谢谢!这对我来说不太起作用,我得到了一个登录 shell,但它是批处理的(ENVIRONMENT=BATCH)。我仍然需要弄清楚如何默认获取 ENVIRONMENT=INTERACTIVE - Walter Nissen

52

只需将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。因此,将执行任何已设置的用户配置文件。


当您使用命令面板访问此文件并插入此设置时,["-l"]周围的括号丢失了。当我添加了括号后,它对我起作用了,现在osx zsh shell正在加载我的常规环境。 - Merlin
为了让它也在工作区目录中启动,我将以下内容添加到我的 ~/.bash_profile 的末尾: [[ -n "${VSCODE_IPC_HOOK_CLI}" ]] && cd "${OLDPWD}" - aizimmer
11
VSCode已弃用"terminal.integrated.shellArgs.osx",建议使用配置文件(profiles)代替。我建议读者查看下面的回答。 - ahaurat

28

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

    "terminal.integrated.inheritEnv": false

默认情况下设置为 false。在我的情况下,将其更改为 true 解决了问题。


天啊,我找了好久才找到这个。谢谢! - Niki
2
这也给了我所需的提示。实际上,现在 terminal.integrated.inheritEnv 的默认值是 true,请参见 https://code.visualstudio.com/updates/v1_57#_improved-launching-with-clean-environment - MarcusS

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 Code 打开 CentOS WSL 中的远程 dev env。

因此,解决这种情况的配置有点不同。 在 IDE 中打开设置。然后在顶部选择 "Remote [WSL: XXX]"。

enter image description here

向下滚动到“集成” -> “配置文件:Linux”,并在settings.json中单击编辑。 {{link1:输入图像描述}}

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

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

保存设置文件,下次打开终端时将遵循您的~/.bash_profile。 注意:我的~/.bash_profile已经有了别人推荐添加的加载bashrc文件的相同行。


此外,如果对人们有用的话。 在WSL文件系统上设置文件的路径是:/root/.vscode-server/data/Machine/settings.json - KrisTC
第一次打开WSL终端时,此设置会被忽略。废弃的设置通过 "terminal.integrated.shellArgs.linux": ["-l"] 工作。来源:https://github.com/microsoft/vscode-remote-release/issues/6096#issuecomment-1506941761 - Joshua Graham

2

"terminal.integrated.shellArgs.osx": ["-l"]已经过时了。

我个人想使用.bash_profile,所以我创建了一个包含以下内容的.bashrc文件:

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

然后我不得不进行一次完整的计算机重启(仅重新启动VS代码无效)。


1
第一行应该是 if [ -f ~/.bash_profile ]; then 吗?从同一个文件中测试文件的存在并没有太多意义。 - tel
@tel 不错,很难说,我只知道这对我起作用了。 - Kevin Danikowski
1
+1 针对全面计算机重启的提醒。之前所有的方法都无法解决问题,直到进行了全面重启。我很想知道为什么这种方法能够解决问题,而注销和重新登录则不能。 - nschmeller
1
奇怪的是,这对我来说没有重新启动或重新加载VSCode就可以工作。 - DiegoSalazar

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 OS。如果在其他平台上,请将osx更改为linuxwindows
  "terminal.integrated.profiles.osx": {
        "bash": {
          "path": "bash",
          "args": ["-i"]
        }
    }

1

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

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

因此,当你使用 bash -l 启动登录 shell 时,它不会读取 ~/.bashrc 文件。然而,当你使用 bash -i 启动交互式 shell 时,它会读取并执行 ~/.bashrc 文件中的命令。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接