我有两个bash
数组,例如:
arr1=( 1 2 3 )
arr2=( 1 2 A )
我想使用比较它们diff
。我如何将数组当作文件的内容来传递?
我尝试了一些变体,但都失败了:
diff -y <$( echo ${arr1[@]} | tr ' ' '\n' ) <$( echo ${arr2[@]} | tr ' ' '\n' )
diff -y <${arr1[@]} <${arr2[@]}
diff -y $(<${arr2[@]}) $(<${arr1[@]})
diff -y <<<"$( echo ${arr1[@]} | tr ' ' '\n' )" \
<<<"$( echo ${arr2[@]} | tr ' ' '\n' )"
所需的输出将是 的预期输出diff -y
,如果我将数组存储到文件 a 和 b 中,我会得到该输出:
diff a b
1 1
2 2
3 | A
(减少可读空间)
出于速度原因,我想避免编写中间文件,尽管我知道tmpfs
伪文件是基于 RAM 的解决方法。
答案1
使用printf
和流程替代
diff -y <(printf '%s\n' "${arr1[@]}") <(printf '%s\n' "${arr2[@]}")
1 1
2 2
3 | A