【问题标题】:WSL bash in Visual Studio Code running .bashrc but not .bash_profile运行 .bashrc 但不运行 .bash_profile 的 Visual Studio Code 中的 WSL bash
【发布时间】:2022-08-15 23:26:49
【问题描述】:

直到最近,当我在 Visual Studio Code 中启动集成终端时,bash 会同时运行我的 .bashrc 和 .bash_profile 文件。但现在它只运行 .bashrc 文件。我需要它来运行两者。

一些细节:VSC 1.70.1(最新),带有“远程 - SSH”扩展,在安装了 WSL 2 的 Windows 10(更新)下运行。当我启动 Microsoft 终端时,它同时运行 .bashrc 和 .bash_profile,但 VSC 的集成终端只运行前者。这可能意味着我有 s.t.我的 VSC 配置错误,但那里的设置似乎经常更改,很难跟上。相关部分似乎成为

\"terminal.integrated.defaultProfile.windows\": \"WSL\",
\"terminal.integrated.profiles.windows\": {
    \"bash\":{
           \"path\": \"C:\\\\Windows\\\\System32\\\\bash.exe\",
           \"args\": [\"-l\"]
    },
    \"WSL\": {
           \"path\": \"C:\\\\WINDOWS\\\\System32\\\\wsl.exe\",
           \"args\": [ ],
           \"icon\": \"terminal-ubuntu\"
    }
},

但这不起作用,我在两个“args”参数上尝试过的任何变体也不起作用,也没有将defaultProfile更改为\"bash\"而不是\"WSL\"

在我放弃并将所有启动设置放入我的 .bashrc 文件并摆脱我的 .bash_profile 文件之前,我还缺少什么?

  • 我对 .bash_profile 的理解是,您在其中设置或更改的内容仅在登录时设置,而 .bashrc 文件您可以进行任何您想要的更改并获取文件以获取更新到您的会话中。我只使用 bashrc 文件。
  • 我承认对 .bashrc 和 .bash_profile 之间的差异感到困惑;一方面,我不确定什么是“登录”。从终端的工作方式来看(微软以及我多年来使用的其他终端),无论何时启动一个新终端(包括选项卡式终端中的一个新选项卡),至少\'s 当 bash_profile 被读取时。除了在 VSC 中。但是,是的,也许我应该只使用 bashrc 文件来解决这个问题......
  • 这个想法是.bash_profile(传统上在 UNIX 上)在您登录时运行一次(并设置可以由子进程继承的环境变量),而 .bashrc 在您每次启动交互式 shell 时运行(并将状态设置为那个单独的外壳)。这是bash -lbash -i 的预期用例之间的区别。 (请注意,MacOS 不遵循此约定;在那里,每个新的终端选项卡都有自己的登录 shell)。
  • @CharlesDuffy:我听说过这种解释,但不知何故我并没有理解它。如果我在使用 WSL(Linux 的 Windows 子系统)的 Windows 上,那么“登录”是什么意思?我很确定 .bash_profile 在我登录 Windows 时不会运行。它确实在 MsTerminal 中运行每一个我启动一个新终端的时候,但是当我在 VSC 中启动一个新终端时它永远不会运行。术语和 VSC 对“登录”的含义有不同的概念吗?无论如何,听起来最好的办法是废弃我的 .bash_profile 并将其全部放入我的 .bashrc 中。
  • 我在谈论 UNIX 传统上如何作为背景工作来解释为什么 bash 有这两个配置文件。我的评论中没有任何内容旨在描述 WSL 的实际行为。也就是说,考虑保留这两个文件,但从.bashrc 采购.bash_profile

标签: bash visual-studio-code terminal windows-subsystem-for-linux


【解决方案1】:

即使升级到 1.70.1 后我也无法重现此问题,因此我建议您尝试以下方法:

  • 尝试

     "path": "C:\\WINDOWS\\System32\\wsl.exe",
     "args": ["-e", "bash", "-li"]
    

    确保登录 shell 已启动

  • 如果升级后这种情况发生了变化,请阅读Help -> Release Notes 以找出可能的原因,例如Shell integration is now enabled by default,所以你可以试试"terminal.integrated.shellIntegration.enabled": false

  • 同步/备份您的设置,重新安装 vanilla VSC,检查行为是否仍然相同

【讨论】:

    【解决方案2】:

    UNIX 上的历史假设是,.bash_profile 在您登录时运行一次,然后.bashrc 在每个新 shell 中运行。因此,子进程继承的东西(如环境变量)进入.bash_profile,因此它们每次登录只能运行一次(通常,作为xinit的父进程或其他GUI启动),而需要的东西在.bashrc 中为每个新shell 单独配置(如别名、非导出函数、提示设置等)。

    但是,如果您处于在会话创建期间未运行 .bash_profile 的环境中,您可能希望在 .bashrc 操作期间调用它(因此它会在设置任何新的交互式 shell 期间发生)。考虑将以下内容添加到您的 .bashrc:

    # source .bash_profile if this has not previously been done
    if ! [[ $profile_already_sourced ]]; then
      if [[ -e ~/.bash_profile ]]; then
        . ~/.bash_profile
        export profile_already_sourced=1
      fi
    fi
    

    【讨论】:

      猜你喜欢
      • 2021-03-05
      • 2018-11-03
      • 1970-01-01
      • 2021-12-15
      • 2016-10-31
      • 2021-04-28
      • 1970-01-01
      • 2022-12-16
      相关资源
      最近更新 更多