Skip to content

Commit fe35b65

Browse files
Merge branch 'main' into carpet-plotly-go
2 parents c4a9303 + 778865c commit fe35b65

File tree

23 files changed

+1667
-2
lines changed

23 files changed

+1667
-2
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,222 @@
1+
---
2+
Title: 'Looping statements'
3+
Description: 'Executes commands repeatedly until a condition is met.'
4+
Subjects:
5+
- 'Bash/Shell'
6+
- 'Computer Science'
7+
Tags:
8+
- 'Automation'
9+
- 'Command Line'
10+
- 'Control Flow'
11+
- 'Loops'
12+
CatalogContent:
13+
- 'learn-the-command-line'
14+
- 'paths/computer-science'
15+
---
16+
17+
**Looping statements** in Bash are control flow constructs that allow a set of commands to be executed repeatedly until a specific condition is met. They are essential for automating repetitive tasks and processing multiple items efficiently.
18+
19+
Loops perform the same set of commands over and over with slight variations, such as processing files in a directory or performing operations on a list of items. Bash provides three main types of loop structures: `while`, `until`, and `for` loops, along with control statements like `break` and `continue` that modify loop execution. These powerful constructs make shell scripts more versatile and efficient for tasks like file processing, data manipulation, and system administration.
20+
21+
## `while` Loop
22+
23+
A **`while` loop** executes a block of commands repeatedly as long as a specified condition evaluates to true. This type of loop is particularly useful when performing an action until a certain condition changes.
24+
25+
### Syntax
26+
27+
```pseudo
28+
while [ condition ]
29+
do
30+
# Commands to execute
31+
done
32+
```
33+
34+
The condition is evaluated before each iteration. If the condition is true, the commands within the loop body are executed. If the condition is false, the loop terminates and execution continues with the commands after the loop.
35+
36+
**Example:**
37+
38+
```bash
39+
#!/bin/bash
40+
# Count from 1 to 5
41+
counter=1
42+
while [ $counter -le 5 ]
43+
do
44+
echo $counter
45+
counter=$((counter+1))
46+
done
47+
echo "Counting complete!"
48+
```
49+
50+
Output:
51+
52+
```shell
53+
1
54+
2
55+
3
56+
4
57+
5
58+
Counting complete!
59+
```
60+
61+
In this example, the loop initializes a counter to 1, then continues printing and incrementing the counter as long as it's less than or equal to 5.
62+
63+
## `until` Loop
64+
65+
An **`until` loop** is the logical opposite of a while loop. It executes commands repeatedly until a specified condition becomes true. In other words, the loop continues as long as the condition is false.
66+
67+
### Syntax
68+
69+
```pseudo
70+
until [ condition ]
71+
do
72+
# Commands to execute
73+
done
74+
```
75+
76+
The condition is evaluated before each iteration. If the condition is false, the commands within the loop body are executed. Once the condition becomes true, the loop terminates.
77+
78+
**Example:**
79+
80+
```bash
81+
#!/bin/bash
82+
# Count from 5 down to 1
83+
counter=5
84+
until [ $counter -lt 1 ]
85+
do
86+
echo $counter
87+
counter=$((counter-1))
88+
done
89+
echo "Countdown complete!"
90+
```
91+
92+
Output:
93+
94+
```shell
95+
5
96+
4
97+
3
98+
2
99+
1
100+
Countdown complete!
101+
```
102+
103+
This example counts down from 5 to 1, executing the loop until the counter becomes less than 1.
104+
105+
## `for` Loop
106+
107+
A **`for` loop** is designed to iterate over a list of items, executing a set of commands once for each item in the list. This loop type is especially useful for processing arrays, ranges of numbers, file lists, and command outputs.
108+
109+
### Syntax
110+
111+
```pseudo
112+
for variable in [list]
113+
do
114+
# Commands to execute
115+
done
116+
```
117+
118+
The list can be a series of values, a range, filenames with wildcards, or the output of a command. For each item in the list, the variable is set to that value, and the commands in the loop body are executed.
119+
120+
**Example:**
121+
122+
```bash
123+
#!/bin/bash
124+
# Print each fruit in the list
125+
fruits="apple banana orange grape"
126+
for fruit in $fruits
127+
do
128+
echo "Current fruit: $fruit"
129+
done
130+
echo "All fruits processed!"
131+
```
132+
133+
Output:
134+
135+
```shell
136+
Current fruit: apple
137+
Current fruit: banana
138+
Current fruit: orange
139+
Current fruit: grape
140+
All fruits processed!
141+
```
142+
143+
In this example, the `for` loop iterates through each word in the `fruits` variable, assigning it to the `fruit` variable, and then executes the `echo` command.
144+
145+
## The `break` Statement
146+
147+
The **`break`** statement allows for exiting a loop immediately, regardless of whether the loop condition is still true. It terminates the innermost loop and execution continues with the command following the loop.
148+
149+
**Example:**
150+
151+
```bash
152+
#!/bin/bash
153+
# Find a specific file in a list
154+
files="document.txt image.jpg script.sh config.ini"
155+
search_file="script.sh"
156+
157+
for file in $files
158+
do
159+
echo "Checking $file..."
160+
if [ "$file" = "$search_file" ]
161+
then
162+
echo "Found $search_file!"
163+
break
164+
fi
165+
done
166+
echo "Search complete."
167+
```
168+
169+
Output:
170+
171+
```shell
172+
Checking document.txt...
173+
Checking image.jpg...
174+
Checking script.sh...
175+
Found script.sh!
176+
Search complete.
177+
```
178+
179+
In this example, the loop terminates as soon as the target file is found, avoiding unnecessary checks of the remaining files.
180+
181+
## The `continue` Statement
182+
183+
The **`continue`** statement skips the remaining commands in the current iteration of a loop and moves to the next iteration. Unlike break, continue does not terminate the loop; it only skips the rest of the current cycle.
184+
185+
**Example:**
186+
187+
```bash
188+
#!/bin/bash
189+
# Process only even numbers
190+
for num in {1..10}
191+
do
192+
# Check if number is odd
193+
if [ $((num % 2)) -eq 1 ]
194+
then
195+
echo "Skipping odd number: $num"
196+
continue
197+
fi
198+
199+
echo "Processing even number: $num"
200+
done
201+
echo "Processing complete."
202+
```
203+
204+
Output:
205+
206+
```shell
207+
Skipping odd number: 1
208+
Processing even number: 2
209+
Skipping odd number: 3
210+
Processing even number: 4
211+
Skipping odd number: 5
212+
Processing even number: 6
213+
Skipping odd number: 7
214+
Processing even number: 8
215+
Skipping odd number: 9
216+
Processing even number: 10
217+
Processing complete.
218+
```
219+
220+
In this example, the `continue` statement allows the loop to skip processing of odd numbers and focus only on even numbers.
221+
222+
For a comprehensive introduction to command line concepts, consider exploring Codecademy's [Learn the Command Line](https://www.codecademy.com/learn/learn-the-command-line) course.

0 commit comments

Comments
 (0)