I have two bash
arrays, say:
arr1=( 1 2 3 )
arr2=( 1 2 A )
and I want to compare them using diff
. How could I pass the arrays as if they were the contents of a file?
I tried a few variations, but all failed:
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' )"
Desired output would be the expected from diff -y
, which I get if I store the arrays into files a and b:
diff a b
1 1
2 2
3 | A
(less spaces for readability)
I would like to avoid writing intermediate files for speed reasons, although I am aware of tmpfs
pseudo files as RAM-based workaround.
<<<
is implemented by writing a file in/tmp
, and redirecting input from that, unfortunately. One of the inputs could be a pipe, though. – Peter Cordes Aug 17 '15 at 13:52