← Back to Bash Course | Chapter 7: String Manipulation | Lesson 7 of 10

for Loop का उपयोग

एक for loop एक chore chart जैसा है जहां आप list में हर नाम के लिए एक ही task एक बार करते हैं। यह हर item या range के हर number के लिए कुछ repeat करने का आसान तरीका है।

In this page:

  1. for Loop
Syntax
bash
for variable in list; do
  commands
done

for variable in {1..5}; do
  commands
done

for Loop

Bash में एक for loop words की एक list, {1..5} के साथ एक range, या ((i=0; i<n; i++)) के साथ एक C-style numeric range पर iterate करता है। Classic form, for item in list; do ... done, सबसे common style है।

Bash का for कई दूसरी languages में मिलने वाले pure counting loop से ज़्यादा flexible है।

Note: for i in {1..10} बिना किसी C-style loop के एक fixed numeric range पर iterate करने का एक concise तरीका है।

उदाहरण: for Loop

bash
#!/bin/bash
for i in {1..5}; do
    echo "Number: $i"
done

for ((i = 0; i < 3; i++)); do
    echo "C-style: $i"
done
Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the view already swaps in the hi_ translation fields (or blanks these out if untranslated), so this renders correctly for both languages without a lang_code check here. #}
आम गलतियां
  1. for i in {1..$n} लिखना, क्योंकि brace expansion variables expand नहीं करता; seq या एक C-style loop उपयोग करें।
  2. $(ls) पर loop करना जब names में spaces हों, जब for f in * ज़्यादा safe है।
  3. do या done भूल जाना, जो एक syntax error देता है।

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.