linux – 我们如何在Bash中获得两个数组的并集?

前端之家收集整理的这篇文章主要介绍了linux – 我们如何在Bash中获得两个数组的并集?前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我有两个阵列,说:
arr1=("one" "two" "three")
arr2=("two" "four" "six")

在Bash中将这两个数组联合起来的最佳方法是什么?

解决方法

首先,组合数组:
arr3=("${arr1[@]}" "${arr2[@]}")

然后,应用this post中的解决方案对其进行重复数据删除

# Declare an associative array
declare -A arr4
# Store the values of arr3 in arr4 as keys.
for k in "${arr3[@]}"; do arr4["$k"]=1; done
# Extract the keys.
arr5=("${!arr4[@]}")

这假设是bash 4.

原文链接:https://www.f2er.com/linux/394479.html

猜你在找的Linux相关文章