以下内容在 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/