git - 将 git 输出保存到变量

标签 git bash

我的 bash 脚本中有 content= $(git diff --cached $line)

enter image description here

但是当我执行它时,bash 抛出错误

gqqnbig MINGW64 /c/Website/Lender (master)
$ ./hook.sh
Admin/Xpress/BusinessAccountTypeRole_Edit.aspx
diff: unknown option -- git
diff: Try 'diff --help' for more information.

为什么 $(git diff --cached $line) 不起作用,如何解决?

最佳答案

你在等号后面放一个空格。

content= $(git diff --cached $line)
        ^
      there

那个空格意味着 Bash 将环境变量 content 设置为该行其余部分指定的命令的空字符串,而不是将 shell 变量 content 设置为运行命令的结果。

关于git - 将 git 输出保存到变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46615962/

相关文章:

git 推送时的默认参数

git - 从 git repo 中删除文件夹的所有痕迹

linux - 从文件的特定行执行命令

c - 是否可以让 bash 检测 C 中的编译错误?

bash - 如何在 Bash 中并行运行给定函数?

git - 我可以同时在 SVN 和 GIT 存储库上共享同一个 eclipse 项目吗?

git - 如何让 git grep 显示在终端屏幕的顶部而不是底部?

Git:如何比较工作目录中的文件与早期提交中的相同文件?

bash - Bash 是否有用于递归函数调用的私有(private)堆栈帧?

bash - 观看流程替换