Bash 与 Zsh,git grep 在 bash for 循环中工作,而不是在 zsh for 循环中工作?

Bash vs. Zsh, git grep works in a bash for loop but not a zsh for loop?

以下在 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 的使用——这意味着您的参数被视为文字字符串,而不是正则表达式,因此您不能使用无效正则表达式语法的文件名。