2
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

シェルスクリプトで配列の重複を消す際の備忘録

Posted at

シェルスクリプトで配列の重複を消す際の備忘録です

以下のようにsort -u(--unique )を使用すればOK

test.sh
array=(foo bar foo baz bar foo)
array2=()
i=0
while read -r x; do
  array2[i++]="$x"
done < <(printf '%s\n' "${array[@]}" | sort -u)

echo "array --->"
echo "${array[@]}"
echo "array2 --->"
echo "${array2[@]}"

実行結果

$ bash test.sh
array --->
foo bar foo baz bar foo
array2 --->
bar baz foo

参考

2
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?