0Pricing
Linux Command Line Mastery · Lekcja

Pętle: `for`, `while`, `until`

Zautomatyzują Państwo powtarzalne zadania, opanowując różne rodzaje pętli w skryptach powłoki.

Pętle: `for`, `while`, `until` to bezpłatna lekcja Linux Command Line Mastery na CoddyKit. To lekcja 3 z 4. Możesz przeczytać całą lekcję poniżej za darmo — a potem ćwiczyć ją interaktywnie w przeglądarce z wbudowanym edytorem kodu i tutorem AI dostępnym 24/7. To część ścieżki edukacyjnej Linux Command Line Mastery, a Twój postęp synchronizuje się między webem a aplikacją CoddyKit. Kurs Linux Command Line Mastery zawiera 4 lekcji w sumie.

Części tej lekcji nie zostały jeszcze przetłumaczone i są wyświetlane po angielsku.

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!

Często zadawane pytania

Czy lekcja „Pętle: `for`, `while`, `until`” jest bezpłatna?

Tak — pełny tekst „Pętle: `for`, `while`, `until`” jest dostępny za darmo tutaj w sieci. Aby ćwiczyć ją interaktywnie (wbudowany edytor kodu i tutor AI dostępny 24/7) i odblokować resztę kursu Linux Command Line Mastery, przejdź na CoddyKit PRO. Kurs Linux Command Line Mastery zawiera 4 lekcji w sumie.

Co nauczysz się w „Pętle: `for`, `while`, `until`”?

Zautomatyzują Państwo powtarzalne zadania, opanowując różne rodzaje pętli w skryptach powłoki. Ćwiczysz Linux Command Line Mastery z praktycznym kodem, który uruchamiasz bezpośrednio w przeglądarce, a tutor AI dostępny 24/7 odpowiada na Twoje pytania podczas pracy nad lekcją.

Czy potrzebuję doświadczenia, aby zacząć Linux Command Line Mastery?

Nie wymagamy żadnego doświadczenia. Linux Command Line Mastery w CoddyKit jest strukturyzowany dla początkujących i zaawansowanych użytkowników, więc możesz zacząć tutaj lub od początku i uczyć się w swoim tempie. To lekcja 3 z 4.

Ile czasu zajmuje lekcja „Pętle: `for`, `while`, `until`”?

Większość lekcji CoddyKit trwa około 5–10 minut. Każda lekcja to mały, interaktywny krok, dzięki czemu robisz systematyczne postępy i zawsze wracasz dokładnie do tego samego miejsca — na webie i w aplikacji.

Czy mogę pisać i uruchamiać kod w tej lekcji Linux Command Line Mastery?

Tak. Każda lekcja Linux Command Line Mastery zawiera wbudowany edytor kodu, więc piszesz i uruchamiasz prawdziwy kod bezpośrednio w przeglądarce i od razu otrzymujesz sprzężenie zwrotne od AI — bez konfiguracji na komputerze.

Wszystkie lekcje w tym kursie

  1. Zmienne, wejście i wyjście
  2. Instrukcje warunkowe: `if`, `else`, `case`
  3. Pętle: `for`, `while`, `until`
  4. Funkcje i argumenty w skryptach powłoki
← Powrót do Linux Command Line Mastery