$ A=($(echo $"${A[@]}" | sed 's/ /\n/g' | sort | uniq))
$ B=($(echo $"${B[@]}" | sed 's/ /\n/g' | sort | uniq))
Intersection:
$ echo $"${A[@]} ${B[@]}" | sed 's/ /\n/g' | sort | uniq -dIf you want to store the elements in another array:
intersection_set=($ intersection_set=$(echo $"${A[@]} ${B[@]}" | sed 's/ /\n/g' | sort | uniq -d)) $ echo $intersection_set"${intersection_set[@]} (elements count: ${#intersection_set[@]})" vol-175a3b54 vol-71600106 vol-98c2bbef (elements count: 3)uniq -dmeans show only duplicates (I think,uniqis rather fast because of its realisation: I guess that it is done withXORoperation)print duplicate lines.Get the list of elements that appear in
Band are not available inA, i.e.B\A$ echo $"${A[@]} ${B[@]}" | sed 's/ /\n/g' | sort | uniq -d | xargs echo $"${B[@]}" | sed 's/ /\n/g' | sort | uniq -uuniq -umeans only print unique lines.Or, with saving in a variable:
subtraction_set=($ subtraction_set=$(echo $"${A[@]} ${B[@]}" | sed 's/ /\n/g' | sort | uniq -d | xargs echo $"${B[@]}" | sed 's/ /\n/g' | sort | uniq -u)) $ echo $subtraction_set"${subtraction_set[@]} (elements count: ${#subtraction_set[@]})" vol-27991850 vol-2a19386a vol-615e1222 vol-7320102b vol-8f6226cc vol-b846c5cf vol-e38d0c94 (elements count: 7)Thus, at first we have got intersection of
AandB(which is simply the set of duplicates between them), say it isA/\B, and then we used operation of inverting intersection ofBandA/\B(which is simply only unique elements), so we getB\A = ! (B /\ (A/\B)).