0Pricing
Linux Command Line Mastery · 강의

반복문: `for`, `while`, `until`

셸 스크립팅에서 다양한 유형의 반복문을 익혀 반복 작업을 자동화합니다.

반복문: `for`, `while`, `until`은(는) CoddyKit의 무료 Linux Command Line Mastery 강의입니다. 이것은 4개 중 3번째 강의입니다. 아래에서 전체 강의를 무료로 읽을 수 있으며, 내장 코드 에디터와 24/7 AI 튜터와 함께 브라우저에서 직접 실습할 수 있습니다. 이 강의는 Linux Command Line Mastery 학습 경로의 일부이며, 진행 상황이 웹과 CoddyKit 앱에 동기화됩니다. Linux Command Line Mastery 강의에는 총 4개의 강의가 포함되어 있습니다.

이 강의의 일부는 아직 번역되지 않았으며 영어로 표시됩니다.

Why Use Loops?

Imagine you need to perform the same action many times, like saying 'Hello' to everyone in a list, or counting from 1 to 100. Doing this manually would be tedious and error-prone.

This is where loops come in! Loops allow your script to repeat a set of commands multiple times, saving you effort and making your scripts more efficient.

Looping Through Items with `for`

The for loop is perfect when you know exactly what items you want to iterate over. It goes through a list, processing each item one by one.

Its basic syntax looks like this:

  • for item in list; do
  • # commands to run for each item
  • done

The item variable will hold each value from the list in turn.

`for` Loop in Action

Let's see the for loop iterate through a list of names. Each name is assigned to the name variable, and then printed.

Try running this example:

#!/bin/bash

echo "--- Greeting Friends ---"
for name in Alice Bob Charlie;
do
  echo "Hello, $name!"
done
echo "--- All greeted! ---"

`for` Loop for Number Ranges

What if you need to loop a specific number of times, like counting from 1 to 5? You can generate sequences for the for loop:

  • Brace Expansion: {1..5} creates "1 2 3 4 5".
  • seq command: $(seq 1 5) also creates "1 2 3 4 5".

Both are handy for numerical iterations.

Counting with `for` Loop

Here's how you can use brace expansion to count up to a specific number. The loop variable i takes on each number in the sequence.

Try running this example:

#!/bin/bash

echo "Counting up!"
for i in {1..3};
do
  echo "Count: $i"
done
echo "Done counting."

Repeating with `while`

The while loop is used when you want to repeat commands as long as a certain condition remains true. It continuously checks the condition before each iteration.

Its basic structure is:

  • while condition; do
  • # commands to run
  • done

The loop stops as soon as the condition becomes false.

`while` Loop Counter

Let's use a while loop to count. We initialize a variable count, and the loop continues as long as count is less than 3.

Remember to update the variable inside the loop, or it might run forever (an 'infinite loop')!

#!/bin/bash

count=0
echo "Starting while loop..."
while [ $count -lt 3 ]; do
  echo "Current count: $count"
  count=$((count + 1))
done
echo "While loop finished."

Repeating `until` True

The until loop is similar to while, but with an inverse condition. It executes commands until a specified condition becomes true.

Its syntax is:

  • until condition; do
  • # commands to run
  • done

The loop continues as long as the condition is false.

`until` Loop in Action

Here's an until loop waiting for a variable num to reach 3. It will keep running as long as num is not greater than or equal to 3.

Try running this example:

#!/bin/bash

num=0
echo "Starting until loop..."
until [ $num -ge 3 ]; do
  echo "Current number: $num"
  num=$((num + 1))
done
echo "Until loop finished."

When to Use Which Loop?

Choosing the right loop makes your scripts clearer and more efficient:

  • for loop: Best for iterating over a known list of items (files, names, numbers in a range).
  • while loop: Ideal when you need to repeat commands as long as a condition holds true (e.g., reading lines from a file, waiting for user input, counting).
  • until loop: Useful when you want to repeat commands until a specific condition becomes true (e.g., waiting for a process to finish, retrying an operation).

Loop Challenge

Let's test your understanding of shell loops.

Loops: A Quick Recap

Well done! You've learned the fundamental looping constructs in shell scripting:

  • The for loop iterates over a fixed list of items or a range.
  • The while loop repeats commands as long as a condition is true.
  • The until loop repeats commands until a condition becomes true.

These loops are essential for automating repetitive tasks and making your scripts dynamic and powerful. Keep practicing!

자주 묻는 질문

“반복문: `for`, `while`, `until`” 강의는 무료인가요?

네 — “반복문: `for`, `while`, `until`” 전체 내용을 이 웹사이트에서 무료로 읽을 수 있습니다. 인터랙티브하게 실습하려면(내장 코드 에디터와 24/7 AI 튜터), CoddyKit PRO로 업그레이드하면 Linux Command Line Mastery 강의 전체를 잠금 해제할 수 있습니다. Linux Command Line Mastery 강의에는 총 4개의 강의가 포함되어 있습니다.

“반복문: `for`, `while`, `until`”에서 뭘 배우나요?

셸 스크립팅에서 다양한 유형의 반복문을 익혀 반복 작업을 자동화합니다. 브라우저에서 직접 실행하는 실습 코드로 Linux Command Line Mastery을(를) 배우며, 24/7 AI 튜터가 강의를 진행하면서 질문에 답변해줍니다.

Linux Command Line Mastery을(를) 시작하는 데 경험이 필요한가요?

사전 경험은 필요하지 않습니다. CoddyKit의 Linux Command Line Mastery은(는) 초급자부터 고급 학습자까지를 위해 구성되어 있으므로, 여기서 시작하거나 처음부터 시작할 수 있으며 자신의 속도대로 진행할 수 있습니다. 이것은 4개 중 3번째 강의입니다.

“반복문: `for`, `while`, `until`” 강의는 얼마나 걸리나요?

대부분의 CoddyKit 강의는 약 5~10분이 소요됩니다. 각 강의는 간결하고 인터랙티브하여 꾸준한 진행이 가능하며, 웹과 앱에서 중단한 부분부터 바로 시작할 수 있습니다.

이 Linux Command Line Mastery 강의에서 코드를 작성하고 실행할 수 있나요?

네. 모든 Linux Command Line Mastery 강의에는 내장 코드 에디터가 포함되어 있으므로, 브라우저에서 바로 실제 코드를 작성하고 실행한 후 즉시 AI 피드백을 받을 수 있습니다 — 로컬 설정이 필요 없습니다.

이 강의의 모든 강의

  1. 변수, 입력 및 출력
  2. 조건문: `if`, `else`, `case`
  3. 반복문: `for`, `while`, `until`
  4. 셸 스크립트의 함수와 인수
← Linux Command Line Mastery(으)로 돌아가기