Skip to content

Commit 4f0a496

Browse files
authored
Merge pull request #1501 from alexhroom/simplify-loop-example-2
Simplifies second example of loops
2 parents fe72161 + 0f83905 commit 4f0a496

File tree

1 file changed

+4
-32
lines changed

1 file changed

+4
-32
lines changed

episodes/05-loop.md

Lines changed: 4 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -415,7 +415,7 @@ Given the output from the `cat` command has been redirected, nothing is printed
415415
::::::::::::::::::::::::::::::::::::::::::::::::::
416416

417417
Let's continue with our example in the `shell-lesson-data/exercise-data/creatures` directory.
418-
Here's a slightly more complicated loop:
418+
Here's another example:
419419

420420
```bash
421421
$ for filename in *.dat
@@ -428,37 +428,9 @@ $ for filename in *.dat
428428
The shell starts by expanding `*.dat` to create the list of files it will process.
429429
The **loop body**
430430
then executes two commands for each of those files.
431-
The first command, `echo`, prints its command-line arguments to standard output.
432-
For example:
433-
434-
```bash
435-
$ echo hello there
436-
```
437-
438-
prints:
439-
440-
```output
441-
hello there
442-
```
443-
444-
In this case,
445-
since the shell expands `$filename` to be the name of a file,
446-
`echo $filename` prints the name of the file.
447-
Note that we can't write this as:
448-
449-
```bash
450-
$ for filename in *.dat
451-
> do
452-
> $filename
453-
> head -n 100 $filename | tail -n 20
454-
> done
455-
```
456-
457-
because then the first time through the loop,
458-
when `$filename` expanded to `basilisk.dat`, the shell would try to run `basilisk.dat` as
459-
a program.
460-
Finally,
461-
the `head` and `tail` combination selects lines 81-100
431+
In the first command, `$filename` is expanded to the name of the file,
432+
so `echo $filename` prints the name of the file.
433+
Then, the `head` and `tail` combination selects lines 81-100
462434
from whatever file is being processed
463435
(assuming the file has at least 100 lines).
464436

0 commit comments

Comments
 (0)