bash
在从 过渡到 的过程中zsh
,我正在比较两个文件夹的内容,并回显共同的文件:
common_files=$(comm -12 <(ls -p "$folder1") <(ls -p "$folder2"))
for f in ${common_files[@]}; do
echo "pass:"
echo "$f"
done
bash
当我得到一个包含单独条目的数组时:
pass:
file1
pass:
file2
pass:
file3
输出zsh
显示所有元素都“合并”为单个数组条目:
pass:
file1
file2
file3
我怎样才能获得与原来相同的行为zsh
?谢谢。
答案1
以下是在 Zsh 中使用数组正确执行此操作的方法:
common-files() {
file-names $1 && local -a files_a=( $reply )
file-names $2 && local -a files_b=( $reply )
# `:*`: Keep only the items common to both.
# `(F)`: Join the items with newlines.
print ${(F)files_a:*files_b}
}
file-names() {
# `.`: Match only files (not dirs).
# `D`: Include dotfiles.
local -a paths=( $1/*(#q.D) )
# `:t`: Keep only the name ("tail") of each file, not its whole path.
reply=( $paths:t )
}