Bash for

1 개요[ | ]

Bash for
Bash for loop

2 for 스타일[ | ]

for (( i = 1; i < 4; i++ )); do
	echo "i = $i"
done
# i = 1
# i = 2
# i = 3
for i in $(seq 1 3); do
	echo "i = $i"
done
for i in `seq 1 3`; do
	echo "i = $i"
done
for i in {1..3}; do
	echo "i = $i"
done
i=1
while [ $i -lt 4 ]; do
	echo "i = $i"
	i=`expr $i + 1`
done
for i in 2 3 5; do
	echo "i = $i"
done
# i = 2
# i = 3
# i = 5

3 foreach 스타일[ | ]

ARR=(bash c python)
for VALUE in "${ARR[@]}"; do
	echo "[$VALUE]"
done
# [bash]
# [c]
# [python]
ARR=("John Smith" "Jane Doe" "Mike Barnes" "Kevin Patterson")
for VALUE in "${ARR[@]}"; do
	echo "[$VALUE]"
done
# [Jonh Smith]
# [Jane Doe]
# [Mike Barnes]
# [Kevin Patterson]
ARR=("John Smith" "Jane Doe" "Mike Barnes" "Kevin Patterson")
for i in "${!ARR[@]}"; do
	echo $i: ${ARR[$i]}
done
# 0: John Smith
# 1: Jane Doe
# 2: Mike Barnes
# 3: Kevin Patterson
ARR=("John Smith" "Jane Doe" "Mike Barnes" "Kevin Patterson")
for i in "${!ARR[@]}"; do
	echo $((i+1)): ${ARR[$i]}
done
# 1: John Smith
# 2: Jane Doe
# 3: Mike Barnes
# 4: Kevin Patterson
ARR=("John Smith" "Jane Doe" "Mike Barnes" "Kevin Patterson")
for i in "${!ARR[@]}"; do
	echo [$((i+1))/${#ARR[@]}] ${ARR[$i]}
done
# [1/4] John Smith
# [2/4] Jane Doe
# [3/4] Mike Barnes
# [4/4] Kevin Patterson

4 같이 보기[ | ]

문서 댓글 ({{ doc_comments.length }})
{{ comment.name }} {{ comment.created | snstime }}