1
1

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 3 years have passed since last update.

シェルで配列を使う方法

Last updated at Posted at 2020-08-07

シェルで配列を使用する

最近使ったシェルの配列化方法を忘れないように残す。

ポイント

・区切り文字 IFSで 指定する
・変数を ()で囲んで 配列化
・配列は [n]指定で 使用する

(ちょっと標語っぽくしてみた)

シェル
# 区切り文字を定義(デフォルトはtab and スペース区切り)
OLD_IFS=${IFS}
IFS=','

# 配列にするデータ
line="a,b,c,d"
echo "line=${line}"

# 配列に格納。変数を()で囲むのがお作法
aline=(${line})

# 配列全体を表示する。
echo "aline[@]=${aline[@]}"

# 配列の先頭列を表示する。
echo "aline[0]=${aline[0]}"

# 配列の4列目を表示する。
echo "aline[3]=${aline[3]}"
結果
[oracle@test ~]$ ./test.sh
line=a,b,c,d
aline[@]=a b c d
aline[0]=a
aline[3]=d
1
1
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
1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?