如何检查arr2 [BASH]中arr1中的元素是否存在?

时间:2019-02-21 02:24:17

标签: bash unix

arr1包含元素“ a”,“ b”,“ c”

arr2包含元素“ b”

如果arr1中存在arr2的元素,则执行...。

我该怎么写?

类似

for x in arr1
do
    if [ ${arr1[x]} == ${arr2[*]} ]
    then ...
    fi
done

1 个答案:

答案 0 :(得分:0)

检查arr1中存在的arr2中的值:

arr1=("a" "b" "c")
arr2=("c" "d" "e")

for i in "${arr2[@]}" 
do 
  for j in "${arr1[@]}" 
  do 
  [[ "$i" == "$j" ]] && echo "$i" 
  done 
done

->打印:c