# 在 codespace 中更改 shell

可以在 codespace 中更改 shell，以保留习惯的设置。

使用 codespace 时，可以使用所选的 shell 打开新的终端窗口，更改新终端窗口的默认 shell，或安装新的 shell。 还可以使用“dotfiles”来配置 shell。

使用默认 dev 容器映像的 codespace 随安装的 `bash`、`zsh` 和 `fish` shell 一起提供。 如果在 VS Code Web 客户端中打开新的 codespace，或通过 SSH 连接到 codespace，终端将打开并默认运行 `bash` 会话。 在 VS Code 桌面应用程序中，默认 shell 取决于本地设置和操作系统。 有关详细信息，请参阅 VS Code 文档中的[终端配置文件](https://code.visualstudio.com/docs/terminal/profiles)。

## 从 VS Code 中的默认 shell 更改

如果不想使用默认 shell，可以使用其他 shell 打开新的终端会话。

1. 如果在 VS Code 中看不到集成终端，请按 <kbd>Ctrl</kbd>+<kbd>\`</kbd>。

2. 在用于打开新终端窗口的 <svg version="1.1" width="16" height="16" viewBox="0 0 16 16" class="octicon octicon-plus" aria-label="plus" role="img"><path d="M7.75 2a.75.75 0 0 1 .75.75V7h4.25a.75.75 0 0 1 0 1.5H8.5v4.25a.75.75 0 0 1-1.5 0V8.5H2.75a.75.75 0 0 1 0-1.5H7V2.75A.75.75 0 0 1 7.75 2Z"></path></svg> 图标的右侧，选择下拉图标。

   ![VS Code 中集成终端的屏幕截图。 加号图标旁边的向下箭头用橙色框出。](/assets/images/help/codespaces/new-shell-session.png)

3. 在下拉菜单中，单击要使用的 shell 的名称。

## 安装新的 shell

如果要在 codespace 中使用尚未在基础映像或开发容器配置中安装的 shell，可以安装新的 shell。

如果使用默认的 dev 容器映像，请查找 Ubuntu Linux 的安装说明。 如果只想在一个会话中使用不同的 shell，可以使用命令行在您工作的代码空间中安装该 shell。 但是，如果在 codespace 中重新生成容器，可能会丢失已安装的程序。 有关详细信息，请参阅“[深入了解 GitHub Codespaces](/zh/enterprise-cloud@latest/codespaces/about-codespaces/deep-dive#about-the-directory-structure-of-a-codespace)”。

可以选择更可靠地安装新 shell：将安装命令包含在点文件存储库中，或作为生命周期命令（如 `postCreateCommand`）包含在 `devcontainer.json` 文件中。 应使用点文件存储库来安装要在自己的所有 codespace 中使用的 shell，以及特定存储库的贡献者应该安装的 shell 的 `devcontainer.json` 文件。 有关详细信息，请参阅 [个性化你帐户的 GitHub Codespaces](/zh/enterprise-cloud@latest/codespaces/setting-your-user-preferences/personalizing-github-codespaces-for-your-account#dotfiles) 和 [开发容器简介](/zh/enterprise-cloud@latest/codespaces/setting-up-your-project-for-codespaces/adding-a-dev-container-configuration/introduction-to-dev-containers)。

### 为新的 shell 添加 VS Code 终端配置文件

VS Code 会自动检测大多数标准 shell 并将其添加为终端配置文件，因此你可以使用已安装的 shell 轻松打开新的终端窗口。

如果你安装的 shell 未被自动检测到，你可以将新的终端配置文件添加到用户设置。 此设置取决于你的操作系统，因此如果使用的是 VS Code Web 客户端，则应使用 `linux`；如果使用的是桌面应用程序，则应使用本地操作系统。

1. 若要打开 Visual Studio Code Command Palette，请按 <kbd>Command</kbd>+<kbd>Shift</kbd>+<kbd>P</kbd> (Mac) 或 <kbd>Ctrl</kbd>+<kbd>Shift</kbd>+<kbd>P</kbd> (Windows)。

2. 首先键入“用户设置”，然后单击“首选项: 打开用户设置 (JSON)”。

3. 在 `settings.json` 文件的 JSON 对象中，添加如下所示的新属性。 将 `OPERATING-SYSTEM` 替换为相关的操作系统（例如 `linux`、`windows` 或 `osx`），并将 `SHELL` 替换为已安装的 shell。

   ```json copy
   "terminal.integrated.profiles.OPERATING-SYSTEM": {
     "SHELL": {
       "path": "SHELL"
     }
   }
   ```

   例如：

   ```json
   "terminal.integrated.profiles.linux": {
    "csh": {
      "path": "csh"
    }
   }
   ```

4. 保存文件。

可以使用“设置同步”在 VS Code Web 客户端和桌面应用程序中打开的所有 codespace 之间共享这些设置。 如果正在使用的是 Web 客户端，则系统默认禁用“设置同步”，你必须启用“设置同步”才能将更改推送到设置或引入在其他位置所做的新更改。 有关详细信息，请参阅“[个性化你帐户的 GitHub Codespaces](/zh/enterprise-cloud@latest/codespaces/setting-your-user-preferences/personalizing-github-codespaces-for-your-account#settings-sync)”。

## 在 VS Code 中设置默认 shell

可以设置默认终端配置文件来选择默认的 shell，用于在 VS Code 中打开所有新终端窗口。 默认终端配置文件取决于操作系统，因此，如果使用的是 VS Code Web 客户端，则可以为 Linux 设置默认配置文件；如果使用的是桌面应用程序，则可以为本地操作系统设置默认配置文件。

> \[!NOTE]
> 无论默认配置文件如何，在 Web 客户端中打开的代码空间最初始终以 `bash` 会话运行。

1. 若要打开 Visual Studio Code Command Palette，请按 <kbd>Command</kbd>+<kbd>Shift</kbd>+<kbd>P</kbd> (Mac) 或 <kbd>Ctrl</kbd>+<kbd>Shift</kbd>+<kbd>P</kbd> (Windows)。

2. 首先键入“用户设置”，然后单击“首选项: 打开用户设置 (JSON)”。

3. 在 JSON 对象中，若要为相关操作系统设置默认 shell，请添加行或编辑现有行，如下所示。

   ```json
   "terminal.integrated.defaultProfile.OPERATING-SYSTEM": "SHELL"
   ```

   例如：

   ```json copy
   {
      "terminal.integrated.defaultProfile.osx": "zsh",
      "terminal.integrated.defaultProfile.linux": "bash",
      "terminal.integrated.defaultProfile.windows": "PowerShell"
   }
   ```

4. 保存 `settings.json` 文件。

可以使用“设置同步”在 VS Code Web 客户端和桌面应用程序中打开的所有 codespace 之间共享这些设置。 如果正在使用的是 Web 客户端，则系统默认禁用“设置同步”，你必须启用“设置同步”才能将更改推送到设置或引入在其他位置所做的新更改。 有关详细信息，请参阅“[个性化你帐户的 GitHub Codespaces](/zh/enterprise-cloud@latest/codespaces/setting-your-user-preferences/personalizing-github-codespaces-for-your-account#settings-sync)”。

## 通过 SSH 设置默认 shell

通过 SSH 从命令行连接到 codespace 时，默认连接到 codespace 中的 `bash` 会话。

如果为 GitHub Codespaces 启用了 dotfiles 存储库，可以在您的 dotfiles 中将命令添加到安装脚本（如 `install.sh`），以更改连接到的默认 shell。 有关详细信息，请参阅 [将 GitHub Codespaces 与 GitHub CLI 配合使用](/zh/enterprise-cloud@latest/codespaces/developing-in-a-codespace/using-github-codespaces-with-github-cli#ssh-into-a-codespace) 和 [个性化你帐户的 GitHub Codespaces](/zh/enterprise-cloud@latest/codespaces/setting-your-user-preferences/personalizing-github-codespaces-for-your-account#dotfiles)。 例如，以下命令将默认 shell 更改为 `zsh`。

```shell copy
sudo chsh "$(id -un)" --shell "/usr/bin/zsh"
```

如果要使用 codespace 中默认未安装的默认 shell，或者确保拥有最新版本的 shell，则可先安装 shell。

```shell copy
sudo apt-get update -y
sudo apt-get install -y csh
sudo chsh "$(id -un)" --shell "/usr/bin/csh"
```

> \[!NOTE]
> 如果创建新的 codespace（例如，使用 `gh codespace create` 创建），则必须等待足够长的时间确保脚本已完成运行，然后才能通过 SSH 连接到 codespace。 如果脚本尚未完成运行，你将连接到默认的 `bash` 会话。

连接到 codespace 后，对于大多数 shell，你都可以使用 `readlink /proc/$$/exe` 命令检查是否正在运行正确的 shell。

## 配置 shell

对于大多数 shell，你可以选择通过配置文件（如 `.bashrc`）来用首选设置配置 shell。 这些设置可以包括别名和环境变量等内容。

默认情况下，codespace 包含预安装的 shell 的预定义配置。 例如，codespace 中的主目录包含 `.bashrc` 和 `.zshrc` 文件。 可以更改这些文件的内容，然后使用命令（如 `source ~/.bashrc`）更新 shell 配置。 但是，如果在 codespace 中重新生成容器，将丢失对这些文件所做的任何更改。 有关详细信息，请参阅“[深入了解 GitHub Codespaces](/zh/enterprise-cloud@latest/codespaces/about-codespaces/deep-dive#about-the-directory-structure-of-a-codespace)”。

通常，应使用 dotfiles 仓库来配置 shell，使其符合个人偏好的设置。 点文件中的设置适用于你创建的所有 codespace，并在容器重新生成后保留。 有关详细信息，请参阅“[个性化你帐户的 GitHub Codespaces](/zh/enterprise-cloud@latest/codespaces/setting-your-user-preferences/personalizing-github-codespaces-for-your-account#dotfiles)”。

### `fish` shell 故障排除

`fish` shell 包含基于 Web 的配置接口。 可以使用 `fish_config` 命令启动本地 Web 服务器并启动此接口，然后执行更改终端提示或查看环境变量等操作。

可以在 codespace 中对 `fish` 使用基于 Web 的接口。 但是，VS Code 的集成终端中的颜色设置取决于所选的 VS Code 主题，并且不能通过在 `fish_config` 接口中设置新主题来替代这些设置。

当 `fish` 启动本地服务器时，GitHub Codespaces 提供的指向转发端口的默认链接无效。 例如，如果在弹出消息上单击“在浏览器中打开”，则会转到错误页。

访问 `fish_config` 的网页版界面：

1. 在运行 `fish` 会话的终端中，输入 `fish_config`。

2. 在终端输出中，按住 <kbd>Command</kbd> 键并单击或按住 <kbd>Ctrl</kbd> 键并单击，打开指向 `web_config` HTML 文件的链接。

   ```shell
   $ fish_config
   Web config started at file:///tmp/web_config60rc9tr3.html
   Hit ENTER to stop.
   ```

3. 在 `web_config` 文件中，按住 <kbd>Command</kbd> 键并单击或按住 <kbd>Ctrl</kbd> 键并单击，打开指向转发端口的链接。

   ```html
   <body>
     <p><a href="http://localhost:8000/1b9411c2469e392b96df5e5b28da485b/">Start the Fish Web config</a></p>
   </body>
   ```

## 其他阅读材料

* [设置用户首选项](/zh/enterprise-cloud@latest/codespaces/setting-your-user-preferences)
* [管理代码空间](/zh/enterprise-cloud@latest/codespaces/managing-your-codespaces)