我有两个数组,比如说:
arr1=("one" "two" "three")
arr2=("two" "four" "six")
在 Bash 中合并这两个数组的最佳方法是什么?
最佳答案
首先,组合数组:
arr3=("${arr1[@]}" "${arr2[@]}")
然后,应用来自 this 的解决方案发布以删除重复项:
# 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+。
关于linux - 我们如何在 Bash 中获得两个数组的并集?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33153756/