0
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 1 year has passed since last update.

bash配列

Posted at

概要

Bashの配列についていつも分からなくなるのでメモ的に作成。

配列を定義する

$#{ARRAY[@]}で配列を操作できる。

${ARRAY}だと先頭の要素がとれる。

ARRAY=(1 2 3 4 5)

echo "ARRAY=(1 2 3 4 5)"
echo "LENGTH=${#ARRAY[@]}"

echo "======================"
for ((i=0; i<${#ARRAY[@]}; i++))
do
  echo "\${ARRAY[$i]}=${ARRAY[$i]}"
done

echo "======================"
echo "\${ARRAY}=${ARRAY}"

結果

ARRAY=(1 2 3 4 5)
LENGTH=5
======================
${ARRAY[0]}=1
${ARRAY[1]}=2
${ARRAY[2]}=3
${ARRAY[3]}=4
${ARRAY[4]}=5
======================
${ARRAY}=1

配列の@と*

配列変数を""で括ると@の場合は配列として扱い、*の場合は1つの文字列として扱う

ARRAY=(1 2 3 4 5)

echo "\${ARRAY[@]}=${ARRAY[@]}"
echo "\${ARRAY[*]}=${ARRAY[*]}"

echo "======================"
i=0
for X in "${ARRAY[@]}"
do
  echo "\${ARRAY[$i]}=$X"
  i=$(($i+1))
done

echo "======================"
for X in "${ARRAY[*]}"
do
  echo "\${ARRAY[$i]}=$X"
  i=$(($i+1))
done

結果

${ARRAY[@]}=1 2 3 4 5
${ARRAY[*]}=1 2 3 4 5
======================
${ARRAY[0]}=1
${ARRAY[1]}=2
${ARRAY[2]}=3
${ARRAY[3]}=4
${ARRAY[4]}=5
======================
${ARRAY[5]}=1 2 3 4 5

配列を関数の引数にする

引数全体を配列にする場合は@で指定する

ARRAY1=(1 2 3 4 5)

sub1() {
  SUB_ARRAY=$@
  
  echo "\${SUB_ARRAY[@]}=${SUB_ARRAY[@]}"
  echo "======================"
  for ((i=0; i<${#SUB_ARRAY[@]}; i++))
  do
    echo "\${SUB_ARRAY[$i]}=${SUB_ARRAY[$i]}"
  done
  echo "======================"
}

echo "sub1 \"\${ARRAY1[@]}\""
sub1 "${ARRAY1[@]}"

echo "sub1 \"\${ARRAY1[*]}\""
sub1 "${ARRAY1[*]}"

結果

sub1 "${ARRAY1[@]}"
${SUB_ARRAY[@]}=1 2 3 4 5
======================
${SUB_ARRAY[0]}=1 2 3 4 5
======================
sub1 "${ARRAY1[*]}"
${SUB_ARRAY[@]}=1 2 3 4 5
======================
${SUB_ARRAY[0]}=1 2 3 4 5
======================

1つめの引数に配列を指定する場合は*で指定して""で括る

ARRAY1=(1 2 3 4 5)

sub2() {
  SUB_ARRAY=($1)
  ARG2=$2
  
  echo "\${SUB_ARRAY[@]}=${SUB_ARRAY[@]}"
  echo "======================"
  for ((i=0; i<${#SUB_ARRAY[@]}; i++))
  do
    echo "\${SUB_ARRAY[$i]}=${SUB_ARRAY[$i]}"
  done
  echo "======================"
  echo "\$ARG2=${ARG2}"
}

echo "sub2 \"\${ARRAY1[@]}\" arg2"
sub2 "${ARRAY1[@]}" arg2

echo "sub2 \"\${ARRAY1[*]}\" arg2"
sub2 "${ARRAY1[*]}" arg2

結果

sub2 "${ARRAY1[@]}" arg2
${SUB_ARRAY[@]}=1
======================
${SUB_ARRAY[0]}=1
======================
$ARG2=2
sub2 "${ARRAY1[*]}" arg2
${SUB_ARRAY[@]}=1 2 3 4 5
======================
${SUB_ARRAY[0]}=1
${SUB_ARRAY[1]}=2
${SUB_ARRAY[2]}=3
${SUB_ARRAY[3]}=4
${SUB_ARRAY[4]}=5
======================
$ARG2=arg2

配列を引数1と引数2に渡すこともできる。

ARRAY1=(1 2 3 4 5)
ARRAY2=(a b c d e)

sub2() {
  SUB_ARRAY1=($1)
  SUB_ARRAY2=($2)
  
  echo "\${SUB_ARRAY1[@]}=${SUB_ARRAY1[@]}"
  echo "======================"
  for ((i=0; i<${#SUB_ARRAY1[@]}; i++))
  do
    echo "\${SUB_ARRAY1[$i]}=${SUB_ARRAY1[$i]}"
  done
  echo "======================"
  echo "\${SUB_ARRAY2[@]}=${SUB_ARRAY2[@]}"
  echo "======================"
  for ((i=0; i<${#SUB_ARRAY2[@]}; i++))
  do
    echo "\${SUB_ARRAY2[$i]}=${SUB_ARRAY2[$i]}"
  done
  echo "======================"
}

echo "sub2 \"\${ARRAY1[*]}\" \"\${ARRAY2[*]}\""
sub2 "${ARRAY1[*]}" "${ARRAY2[*]}"

結果

sub2 "${ARRAY1[*]}" "${ARRAY2[*]}"
${SUB_ARRAY1[@]}=1 2 3 4 5
======================
${SUB_ARRAY1[0]}=1
${SUB_ARRAY1[1]}=2
${SUB_ARRAY1[2]}=3
${SUB_ARRAY1[3]}=4
${SUB_ARRAY1[4]}=5
======================
${SUB_ARRAY2[@]}=a b c d e
======================
${SUB_ARRAY2[0]}=a
${SUB_ARRAY2[1]}=b
${SUB_ARRAY2[2]}=c
${SUB_ARRAY2[3]}=d
${SUB_ARRAY2[4]}=e
======================

2重配列 その1

個々の配列を宣言しMAIN_ARRAYには変数名を設定している。
!で変数名から変数を取得して、:N番目:N個で配列の値を取得している。

ARRAY1=(1 2 3 4 5)
ARRAY2=(a b c d e)
MAIN_ARRAY=(ARRAY1[@] ARRAY2[@])

echo ${MAIN_ARRAY[@]}

for ((i=0; i<${#MAIN_ARRAY[@]}; i++))
do
  echo ${MAIN_ARRAY[i]}
  echo ${!MAIN_ARRAY[i]}
  for ((j=0; j<5; j++))
  do
    echo ${!MAIN_ARRAY[i]:j:1}
  done
done

結果

ARRAY1[@] ARRAY2[@]
ARRAY1[@]
1 2 3 4 5
1
2
3
4
5
ARRAY2[@]
a b c d e
a
b
c
d
e

2重配列 その2

個々の配列を定義してMAIN_ARRAYには配列を文字列として指定している。

ARRAY1=(1 2 3 4 5)
ARRAY2=(a b c d e)
MAIN_ARRAY=("${ARRAY1[*]}" "${ARRAY2[*]}")

echo ${MAIN_ARRAY[@]}

for ((i=0; i<${#MAIN_ARRAY[@]}; i++))
do
  echo "\${MAIN_ARRAY[$i]}=${MAIN_ARRAY[i]}"
  SUB_ARRAY=(${MAIN_ARRAY[i]})
  for ((j=0; j<5; j++))
  do
    echo "\${SUB_ARRAY[$j]}=${SUB_ARRAY[j]}"
  done
done

結果

1 2 3 4 5 a b c d e
${MAIN_ARRAY[0]}=1 2 3 4 5
${SUB_ARRAY[0]}=1
${SUB_ARRAY[1]}=2
${SUB_ARRAY[2]}=3
${SUB_ARRAY[3]}=4
${SUB_ARRAY[4]}=5
${MAIN_ARRAY[1]}=a b c d e
${SUB_ARRAY[0]}=a
${SUB_ARRAY[1]}=b
${SUB_ARRAY[2]}=c
${SUB_ARRAY[3]}=d
${SUB_ARRAY[4]}=e
0
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
0
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?