Help us understand the problem. What is going on with this article?

bash で配列比較

More than 3 years have passed since last update.

2つの配列から重複している要素だけを取り出すスクリプトメモ

#!/bin/bash

array1=("hoge" "fuga" "foo" "bar" "barbar" "1" "hogehoge" "temp" "varvar")
array2=("varvar" "hoge" "fuga" "foo" "barbar" "2" "123" "tmp" "varvarvar" "1")

echo ${array1[@]}
echo ${array2[@]}

for p in ${array1[@]}; do
  for q in ${array2[@]}; do
    if [ "${p}" = "${q}" ]; then
      array3+=(${p})
    fi
  done
done


echo ${array3[@]}

実行例
./arraycompare.sh
hoge fuga foo bar barbar 1 hogehoge temp varvar
varvar hoge fuga foo barbar 2 123 tmp varvarvar 1
hoge fuga foo barbar 1 varvar
penguin_dream
フロントエンド(主にjavascript)、プログラミング、インフラ構築、インフラ運用、CIなどのメモ。
Why not register and get more from Qiita?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away