在 windows 机器上的 bash 脚本中找不到 git 命令

git command not found in bash script on windows machine

我正在尝试为一些基本 git 操作编写脚本,但出现问题 git status: command not found

我正在试穿:
- Windows 机器
- bash 脚本
- 运行 来自 git bash
的脚本 - git 安装在 C:\Program Files\Git\mingw64\bin

下面是我正在尝试的示例脚本:

path_to_git='/c/Program Files/Git/mingw64/bin/'
PATH=$PATH:$path_to_git
echo $PATH
export PATH

res=$("git status")
echo $res

请指导我,谢谢

像你一样引用使得 shell 寻找一个名为 git status 而不是 git 的命令并带有参数 status – 去掉双引号:

res=$(git status)

然后不要 echo 没有 引号,否则它会删除所有换行符:

echo "$res"

对于脚本,考虑使用

git status --porcelain

相反(请参阅 documentation),使用更易于解析的版本化输出格式。