Skip to content

Instantly share code, notes, and snippets.

@goddoe
Created April 12, 2022 14:47
Show Gist options
  • Select an option

  • Save goddoe/ac8e39f0c3e992af00897577bdd4f54b to your computer and use it in GitHub Desktop.

Select an option

Save goddoe/ac8e39f0c3e992af00897577bdd4f54b to your computer and use it in GitHub Desktop.
bash for loop
#Reference: https://blog.leocat.kr/notes/2018/02/17/shell-looping-list
$ 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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment