Bash 与 Zsh,git grep 在 bash for 循环中工作,但在 zsh for 循环中不起作用?

标签 bash zsh

以下内容在 bash 中有效,但在 zsh 中无效,我不确定为什么:

user@machine:~/some_git_repo$ for i in $(ls ./conf); do git --no-pager grep $i ; done

zsh shell 对 ./conf 中的每个文件产生以下结果

fatal: command line, 'some_file_name.xml': Unmatched [ or [^

最佳答案

您根本不应该在此处使用命令替换。相反,请使用 glob - 并参见 Why you shouldn't parse the output of ls(1) .

# this works reliably in both bash and zsh
for i in ./conf/*; do git --no-pager grep -F -e "${i##*/}"; done

请注意 grep -F 的使用 - 这意味着您的参数被视为文字字符串,而不是正则表达式,因此您不能使用无效正则表达式语法的文件名。

关于Bash 与 Zsh,git grep 在 bash for 循环中工作,但在 zsh for 循环中不起作用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39417308/

相关文章:

python - 根据多个 CSV 文件中找到的分类单元的频率创建一个表

bash - .bashrc 不是来自 Intellij IDEA 的终端

bash - Bash 中是否有一个钩子(Hook)可以查明 cwd 何时更改?

shell - 什么${(%):-%n} means?

linux - 将参数保存在传递给 shell 脚本的不同变量中

linux - 从字符串中压缩

linux - 确定定义 UNIX 别名的位置

bash - 捕获 CHLD 信号 - ZSH 有效但 ksh/bash/sh 不?

bash - git 的最终别名失败(__git_aliases 命令似乎已被弃用)