【问题标题】:How to setup bash in vscode running in WSL2如何在 WSL2 中运行的 vscode 中设置 bash
【发布时间】:2019-12-09 13:12:02
【问题描述】:

我想在使用 WSL2 的 vscode 中使用 oh-my-bash 之类的东西。不过根据docs

在 WSL 中启动 VS Code Remote 时,不会运行任何 shell 启动脚本。这样做是为了避免针对 shell 调整的启动脚本出现问题。如果您想运行其他命令或修改环境,可以在设置脚本~/.vscode-server/server-env-setup(内部人员:~/.vscode-server-insiders/server-env-setup)中完成。如果存在,则在启动服务器之前处理脚本。

我添加了~/.vscode-server/server-env-setup,根据日志找到并执行了它,但我的 linux 技能非常基础,我不知道如何安装我的配置文件。我试过了

bash ~/.profile

...但这似乎没有任何作用。我也试过了

#!/bin/bash
source ~/.profile

这给了我一个错误/mnt/c/Users/cber/.vscode/extensions/ms-vscode-remote.remote-wsl-0.40.3/scripts/wslServer.sh: 3: /home/cber/.vscode-server/server-env-setup: source: not found

更新

下面回答了如何获取配置文件的问题,但是我让powerline-go 在 WSL2 上的 vs-code 中工作的问题仍然存在,但我将它移到了new question 以关闭这个。

【问题讨论】:

  • bash ~/.profile 将在新的子 shell 中执行您的 .profile 文件,然后退出,有效地丢弃所做的修改。您要查找的关键字是source ~/.profile
  • @Aserre,我也试过source ~./profile,这给了我一个错误/mnt/c/Users/cber/.vscode/extensions/ms-vscode-remote.remote-wsl-0.40.3/scripts/wslServer.sh: 3: /home/cber/.vscode-server/server-env-setup: source: not found。进行了一些搜索并找到了this,所以我尝试将#!/bin/bash 添加为第一行,但在使用源代码时我仍然遇到同样的错误。
  • 你能显示你的/home/cber/.vscode-server/server-env-setup文件的内容吗?你能告诉我们当你把readlink /proc/$$/exe 放在第一行时会发生什么吗?
  • @Aserre,该文件目前看起来像bash #!/bin/bash source ~/.profile ...如果我将其更改为bash readlink /proc/$$/exe source ~/.profile ...它输出/bin/dash

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


【解决方案1】:

为了在当前 shell 中保留您的设置,您需要 source 您的配置,而不是仅仅执行它(有关详细信息,请参阅 this link)。

问题是 vscode 使用dash 来加载你的配置文件,而不是bash

但是,sourcebash 关键字,dash 无法理解。因此,您必须使用更便携的语法.,才能使其与dash 一起使用。

尝试用以下内容替换您的文件(不需要#!/bin/bash):

# if the profile file exists, we source it
if [ -f ~/.profile ]
then
  . ~/.profile
fi

【讨论】:

  • 经过几次尝试后,我意识到我打开了多个 vscode 窗口,我需要在 server-env-setup 脚本运行之前关闭所有这些窗口。在执行此操作后,似乎 vscode 在没有脚本帮助的情况下更新了我的 $PATH 变量......
猜你喜欢
  • 2019-12-02
  • 2022-06-19
  • 2021-12-02
  • 1970-01-01
  • 1970-01-01
  • 2011-12-06
  • 2021-06-14
  • 2023-01-17
  • 2020-10-05
相关资源
最近更新 更多