ℹ️ Skipped - page is already crawled
| Filter | Status | Condition | Details |
|---|---|---|---|
| HTTP status | PASS | download_http_code = 200 | HTTP 200 |
| Age cutoff | PASS | download_stamp > now() - 6 MONTH | 1.8 months ago |
| History drop | PASS | isNull(history_drop_reason) | No drop reason |
| Spam/ban | PASS | fh_dont_index != 1 AND ml_spam_score = 0 | ml_spam_score=0 |
| Canonical | PASS | meta_canonical IS NULL OR = '' OR = src_unparsed | Not set |
| Property | Value |
|---|---|
| URL | https://blogs.kenokivabe.com/article/for-loop-in-bash-basics-and-examples |
| Last Crawled | 2026-02-14 19:27:04 (1 month ago) |
| First Indexed | 2025-08-06 06:33:51 (8 months ago) |
| HTTP Status Code | 200 |
| Meta Title | For Loop in Bash: Basics and Examples - Keno Kivabe |
| Meta Description | null |
| Meta Canonical | null |
| Boilerpipe Text | Repeat commands using classic
for
loops.
Bash scripting is a powerful way to automate tasks in Linux and UNIX-like systems. One staple of any programming language is the ability to repeat actions—enter the
for
loop
. In this article, we'll explore the basics of the
for
loop in Bash, see its syntax, usage patterns, and go through practical examples.
What is a
for
Loop?
A
for
loop allows you to execute a set of commands repeatedly, iterating through a sequence of values. This is especially helpful when you need to manipulate groups of files, process batch jobs, or automate repetitive tasks.
Basic Syntax of
for
Loops in Bash
There are two primary forms of the
for
loop in Bash:
1. List-Based (Classic) Syntax
for VARIABLE in VALUE1 VALUE2 VALUE3; do
commands
done
VARIABLE
: The control variable that holds the current value in the iteration.
VALUE1 VALUE2 VALUE3
: A list of values to iterate over.
commands
: The block of code to execute each iteration.
Example
for color in red green blue; do
echo "Color: $color"
done
Output
:
Color: red
Color: green
Color: blue
2. C-like Syntax (Numeric Loop)
If you want to iterate over a sequence of numbers, use this familiar construct:
for (( initial; condition; increment )); do
commands
done
Example
for (( i=1; i<=5; i++ )); do
echo "Iteration $i"
done
Output
:
Iteration 1
Iteration 2
Iteration 3
Iteration 4
Iteration 5
Common Use Cases
1. Looping Over Files
List all
.txt
files in a directory:
for file in *.txt; do
echo "Found file: $file"
done
2. Processing Arguments
Iterate over command line arguments:
for arg in "$@"; do
echo "Argument: $arg"
done
3. Reading Lines from a File
Read every line from a file:
while IFS= read -r line; do
echo "Line: $line"
done < filename.txt
Or using a
for
loop with
$(cat ...)
(Note: less robust with spaces):
for line in $(cat filename.txt); do
echo "Word or line: $line"
done
4. Ranges with
seq
Iterate over a number range using the
seq
utility:
for i in $(seq 1 5); do
echo "Counting: $i"
done
Tips and Best Practices
Quote variables
: Always quote your variables to prevent word splitting and glob expansion:
"$file"
,
"$var"
.
Be wary with spaces
: Using
for x in $(cat file.txt)
splits on whitespace; for line-by-line reading, prefer
while read
.
Brace Expansion
: For simple numeric or alphabetic ranges, use
{}
:
for i in {1..5}; do
echo "Number: $i"
done
Conclusion
The Bash
for
loop is a fundamental feature that can save you time and optimize your workflows. Whether you’re iterating over files, arguments, or numeric ranges, understanding these looping basics will enhance your scripting skills. Experiment with the examples above to level up your command-line automation! |
| Markdown | [Keno Kivabe](https://blogs.kenokivabe.com/)
# For Loop in Bash: Basics and Examples
*Repeat commands using classic `for` loops.*
***
Bash scripting is a powerful way to automate tasks in Linux and UNIX-like systems. One staple of any programming language is the ability to repeat actions—enter the **`for` loop**. In this article, we'll explore the basics of the `for` loop in Bash, see its syntax, usage patterns, and go through practical examples.
## What is a `for` Loop?
A `for` loop allows you to execute a set of commands repeatedly, iterating through a sequence of values. This is especially helpful when you need to manipulate groups of files, process batch jobs, or automate repetitive tasks.
## Basic Syntax of `for` Loops in Bash
There are two primary forms of the `for` loop in Bash:
### 1\. List-Based (Classic) Syntax
```
for VARIABLE in VALUE1 VALUE2 VALUE3; do
commands
done
```
- `VARIABLE`: The control variable that holds the current value in the iteration.
- `VALUE1 VALUE2 VALUE3`: A list of values to iterate over.
- `commands`: The block of code to execute each iteration.
#### Example
```
for color in red green blue; do
echo "Color: $color"
done
```
**Output**:
```
Color: red
Color: green
Color: blue
```
### 2\. C-like Syntax (Numeric Loop)
If you want to iterate over a sequence of numbers, use this familiar construct:
```
for (( initial; condition; increment )); do
commands
done
```
#### Example
```
for (( i=1; i<=5; i++ )); do
echo "Iteration $i"
done
```
**Output**:
```
Iteration 1
Iteration 2
Iteration 3
Iteration 4
Iteration 5
```
## Common Use Cases
### 1\. Looping Over Files
List all `.txt` files in a directory:
```
for file in *.txt; do
echo "Found file: $file"
done
```
### 2\. Processing Arguments
Iterate over command line arguments:
```
for arg in "$@"; do
echo "Argument: $arg"
done
```
### 3\. Reading Lines from a File
Read every line from a file:
```
while IFS= read -r line; do
echo "Line: $line"
done < filename.txt
```
Or using a `for` loop with `$(cat ...)` (Note: less robust with spaces):
```
for line in $(cat filename.txt); do
echo "Word or line: $line"
done
```
### 4\. Ranges with `seq`
Iterate over a number range using the `seq` utility:
```
for i in $(seq 1 5); do
echo "Counting: $i"
done
```
## Tips and Best Practices
- **Quote variables**: Always quote your variables to prevent word splitting and glob expansion: `"$file"`, `"$var"`.
- **Be wary with spaces**: Using `for x in $(cat file.txt)` splits on whitespace; for line-by-line reading, prefer `while read`.
- **Brace Expansion**: For simple numeric or alphabetic ranges, use `{}`:
```
for i in {1..5}; do
echo "Number: $i"
done
```
## Conclusion
The Bash `for` loop is a fundamental feature that can save you time and optimize your workflows. Whether you’re iterating over files, arguments, or numeric ranges, understanding these looping basics will enhance your scripting skills. Experiment with the examples above to level up your command-line automation\!
[Back to All Articles](https://blogs.kenokivabe.com/) |
| Readable Markdown | *Repeat commands using classic `for` loops.*
***
Bash scripting is a powerful way to automate tasks in Linux and UNIX-like systems. One staple of any programming language is the ability to repeat actions—enter the **`for` loop**. In this article, we'll explore the basics of the `for` loop in Bash, see its syntax, usage patterns, and go through practical examples.
## What is a `for` Loop?
A `for` loop allows you to execute a set of commands repeatedly, iterating through a sequence of values. This is especially helpful when you need to manipulate groups of files, process batch jobs, or automate repetitive tasks.
## Basic Syntax of `for` Loops in Bash
There are two primary forms of the `for` loop in Bash:
### 1\. List-Based (Classic) Syntax
```
for VARIABLE in VALUE1 VALUE2 VALUE3; do
commands
done
```
- `VARIABLE`: The control variable that holds the current value in the iteration.
- `VALUE1 VALUE2 VALUE3`: A list of values to iterate over.
- `commands`: The block of code to execute each iteration.
#### Example
```
for color in red green blue; do
echo "Color: $color"
done
```
**Output**:
```
Color: red
Color: green
Color: blue
```
### 2\. C-like Syntax (Numeric Loop)
If you want to iterate over a sequence of numbers, use this familiar construct:
```
for (( initial; condition; increment )); do
commands
done
```
#### Example
```
for (( i=1; i<=5; i++ )); do
echo "Iteration $i"
done
```
**Output**:
```
Iteration 1
Iteration 2
Iteration 3
Iteration 4
Iteration 5
```
## Common Use Cases
### 1\. Looping Over Files
List all `.txt` files in a directory:
```
for file in *.txt; do
echo "Found file: $file"
done
```
### 2\. Processing Arguments
Iterate over command line arguments:
```
for arg in "$@"; do
echo "Argument: $arg"
done
```
### 3\. Reading Lines from a File
Read every line from a file:
```
while IFS= read -r line; do
echo "Line: $line"
done < filename.txt
```
Or using a `for` loop with `$(cat ...)` (Note: less robust with spaces):
```
for line in $(cat filename.txt); do
echo "Word or line: $line"
done
```
### 4\. Ranges with `seq`
Iterate over a number range using the `seq` utility:
```
for i in $(seq 1 5); do
echo "Counting: $i"
done
```
## Tips and Best Practices
- **Quote variables**: Always quote your variables to prevent word splitting and glob expansion: `"$file"`, `"$var"`.
- **Be wary with spaces**: Using `for x in $(cat file.txt)` splits on whitespace; for line-by-line reading, prefer `while read`.
- **Brace Expansion**: For simple numeric or alphabetic ranges, use `{}`:
```
for i in {1..5}; do
echo "Number: $i"
done
```
## Conclusion
The Bash `for` loop is a fundamental feature that can save you time and optimize your workflows. Whether you’re iterating over files, arguments, or numeric ranges, understanding these looping basics will enhance your scripting skills. Experiment with the examples above to level up your command-line automation\! |
| Shard | 128 (laksa) |
| Root Hash | 11491453369347164128 |
| Unparsed URL | com,kenokivabe!blogs,/article/for-loop-in-bash-basics-and-examples s443 |