[Shell] 배열 loop

by 스뎅(thDeng) on

bash에서 배열 순환(loop)하는 방법 (매번 까먹는다 =_=)

$ cat test.sh
#!/bin/bash

for NAME in "ME" "YOU" "THEM" "ALL"; do
    echo "Name is ${NAME}"
done

PLANETS=( "EARTH" "MARS" "VINUS" )
for PLANET in ${PLANETS[@]}; do
    echo "This is ${PLANET}"
done
for (( i=0; i<${#PLANETS[@]}; i++ )); do
    echo "Planet #$i is ${PLANETS[i]}"
done

$ ./test.sh
Name is ME
Name is YOU
Name is THEM
Name is ALL
This is EARTH
This is MARS
This is VINUS
Planet #0 is EARTH
Planet #1 is MARS
Planet #2 is VINUS

참고

별도로 명시하지 않을 경우, 이 블로그의 포스트는 다음 라이선스에 따라 사용할 수 있습니다: Creative Commons License CC Attribution-NonCommercial-ShareAlike 4.0 International License