Windows(Shell) —— 通过Git Bash和Msys2在Windows上使用ZSH

Windows(Shell) —— 通过Git Bash和Msys2在Windows上使用ZSH

天下人苦cmd久矣(bushi)

Windows自带的cmd和PowerShell在使用上都不如Linux下的shell方便,安装了Git后默认会带有Git Bash(运行时基于msys2-runtime修改而来),只需要再安装一个MSYS2的zsh就能在Windows上通过Git Bash使用zsh了。

Git 安装(简述)

折腾Windows上ZSH的大概都安装过git。如果没,那就在官网下载并安装即可。

安装完成后如果你能正常使用git命令那么你大概率也可以直接使用bash命令来使用Git Bash了。

MSYS2简介

MSYS2是一个在Windows上运行的类Unix环境,如果你想要省事儿的话,也可以直接安装一个MSYS2。MSYS2使用pacman作为包管理器,可以直接使用pacman -S zsh来安装zsh。

但这不是本文的目的。由于Git Bash本身就是基于msys2-runtime修改而来,所以我们可以直接在Git Bash中使用MSYS2的zsh。

ZSH下载“安装”

访问msys2官网的zsh包,找到File:后面的下载链接:https://mirror.msys2.org/msys/x86_64/zsh-5.9.2-1-x86_64.pkg.tar.zst,这是一个压缩包,可以使用Bandizip或者7-Zip等工具解压。

解压后你可能会得到一个压缩包,再解压一次可得到一些文件(夹):

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
zsh-5.9.2-1-x86_64.pkg
├─etc
│ └─zsh
└─usr
│ ├─bin
│ ├─lib
│ │ └─zsh
│ │ └─5.9.2
│ │ └─zsh
│ │ ├─net
│ │ └─param
│ └─share
│ ├─licenses
│ │ └─zsh
│ ├─man
│ │ └─man1
│ └─zsh
│ ├─5.9.2
│ │ └─help
│ ├─functions
│ │ ├─Calendar
│ │ ├─Chpwd
│ │ ├─Completion
│ │ │ ├─Base
│ │ │ ├─Linux
│ │ │ ├─Unix
│ │ │ ├─X
│ │ │ └─Zsh
│ │ ├─Exceptions
│ │ ├─Math
│ │ ├─MIME
│ │ ├─Misc
│ │ ├─Newuser
│ │ ├─Prompts
│ │ ├─TCP
│ │ ├─VCS_Info
│ │ │ └─Backends
│ │ ├─Zftp
│ │ └─Zle
│ ├─scripts
│ └─site-functions
├─.BUILDINFO
├─.INSTALL
├─.MTREE
└─.PKGINFO

其中.开头的4个文件不用管,只需要把etcusr文件夹放到Git Bash的安装目录下就好了。

Git Bash的安装目录在哪里?可以这样找到:Win + R,输入cmd,在弹出的窗口中输入where bash,得到类似C:\Program Files\Git\bin\bash.exe的路径,那么Git Bash的安装目录就是C:\Program Files\Git

你看C:\Program Files\Git目录也有etcusr文件夹,直接把解压后的etcusr文件夹拖拽到这个目录下就可以了,会自动合并子文件夹(如果你不放心也可以手动递归复制文件(夹)到对应的目录)。

现在你在Git Bash中输入zsh,应该会有如下提示,直接按0跳过就行:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
This is the Z Shell configuration function for new users,
zsh-newuser-install.
You are seeing this message because you have no zsh startup files
(the files .zshenv, .zprofile, .zshrc, .zlogin in the directory
~). This function can help you with a few settings that should
make your use of the shell easier.

You can:

(q) Quit and do nothing. The function will be run again next time.

(0) Exit, creating the file ~/.zshrc containing just a comment.
That will prevent this function being run again.

(1) Continue to the main menu.

--- Type one of the keys in parentheses ---

关于怎么打开Git bash,如果你是下载的Git安装包一路安装的话,应该可以在任意目录下右键,选择“在Git Bash中打开”;如果没有类似选项,也可以Win + R输入bash并回车即可。

ZSH的简单配置

git bash默认使用zsh

先把你的输入法调成英文,然后在Git Bash中输入:

1
vim ~/.bashrc

i进入编辑模式,粘贴如下内容:

1
2
3
if [ -t 1 ]; then
exec zsh
fi

Esc退出编辑模式,输入:wq保存并退出。

这段命令的意思如下:[ -t 1 ]表示判断“文件描述符 1(stdout 标准输出)是不是终端”,如果是在交互式终端中运行则条件成立,启动zsh并使用zsh替换当前shell进程,否则(例如重定向到文件等场景)条件不成立就不切到zsh了。

也可以写成:

1
2
3
if [[ $- == *i* ]]; then
exec zsh
fi

$- 是 Bash 的一个特殊变量,里面放着当前 Shell 的各种运行选项。如果这个字符串里面有i(代表interactive)就说明是交互式终端。

后续自定义配置

后面怎么配置就可以跟Linux和Mac上一样了,比如安装个oh-my-zshzsh-autosuggestions之类的。

End

参考文献:

同步发文于CSDN和我的个人博客,原创不易,转载经作者同意后请附上原文链接哦~

千篇源码题解已开源


Windows(Shell) —— 通过Git Bash和Msys2在Windows上使用ZSH
https://blog.letmefly.xyz/2026/09/09/Other-Windows-Shell-UseZSHByGitBashAndMsys2/
作者
发布于
2026年9月9日
许可协议