forked from swcarpentry/shell-novice
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path05-loop.html
1393 lines (1350 loc) · 85.8 KB
/
05-loop.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<!DOCTYPE html>
<!-- START: inst/pkgdown/templates/layout.html --><!-- Generated by pkgdown: do not edit by hand --><html lang="en" data-bs-theme="auto"><head><meta http-equiv="Content-Type" content="text/html; charset=UTF-8"><meta charset="utf-8"><title>The Unix Shell: Loops</title><meta name="viewport" content="width=device-width, initial-scale=1"><script src="assets/themetoggle.js"></script><link rel="stylesheet" type="text/css" href="assets/styles.css"><script src="assets/scripts.js" type="text/javascript"></script><!-- mathjax --><script type="text/x-mathjax-config">
MathJax.Hub.Config({
config: ["MMLorHTML.js"],
jax: ["input/TeX","input/MathML","output/HTML-CSS","output/NativeMML", "output/PreviewHTML"],
extensions: ["tex2jax.js","mml2jax.js","MathMenu.js","MathZoom.js", "fast-preview.js", "AssistiveMML.js", "a11y/accessibility-menu.js"],
TeX: {
extensions: ["AMSmath.js","AMSsymbols.js","noErrors.js","noUndefined.js"]
},
tex2jax: {
inlineMath: [['\\(', '\\)']],
displayMath: [ ['$$','$$'], ['\\[', '\\]'] ],
processEscapes: true
}
});
</script><script src="https://cdnjs.cloudflare.com/ajax/libs/mathjax/2.7.5/MathJax.js" integrity="sha256-nvJJv9wWKEm88qvoQl9ekL2J+k/RWIsaSScxxlsrv8k=" crossorigin="anonymous"></script><!-- Responsive Favicon for The Carpentries --><link rel="apple-touch-icon" sizes="180x180" href="apple-touch-icon.png"><link rel="icon" type="image/png" sizes="32x32" href="favicon-32x32.png"><link rel="icon" type="image/png" sizes="16x16" href="favicon-16x16.png"><link rel="manifest" href="site.webmanifest"><link rel="mask-icon" href="safari-pinned-tab.svg" color="#5bbad5"><meta name="msapplication-TileColor" content="#da532c"><meta name="theme-color" media="(prefers-color-scheme: light)" content="white"><meta name="theme-color" media="(prefers-color-scheme: dark)" content="black"></head><body>
<header id="top" class="navbar navbar-expand-md top-nav software"><svg xmlns="http://www.w3.org/2000/svg" class="d-none"><symbol id="check2" viewbox="0 0 16 16"><path d="M13.854 3.646a.5.5 0 0 1 0 .708l-7 7a.5.5 0 0 1-.708 0l-3.5-3.5a.5.5 0 1 1 .708-.708L6.5 10.293l6.646-6.647a.5.5 0 0 1 .708 0z"></path></symbol><symbol id="circle-half" viewbox="0 0 16 16"><path d="M8 15A7 7 0 1 0 8 1v14zm0 1A8 8 0 1 1 8 0a8 8 0 0 1 0 16z"></path></symbol><symbol id="moon-stars-fill" viewbox="0 0 16 16"><path d="M6 .278a.768.768 0 0 1 .08.858 7.208 7.208 0 0 0-.878 3.46c0 4.021 3.278 7.277 7.318 7.277.527 0 1.04-.055 1.533-.16a.787.787 0 0 1 .81.316.733.733 0 0 1-.031.893A8.349 8.349 0 0 1 8.344 16C3.734 16 0 12.286 0 7.71 0 4.266 2.114 1.312 5.124.06A.752.752 0 0 1 6 .278z"></path><path d="M10.794 3.148a.217.217 0 0 1 .412 0l.387 1.162c.173.518.579.924 1.097 1.097l1.162.387a.217.217 0 0 1 0 .412l-1.162.387a1.734 1.734 0 0 0-1.097 1.097l-.387 1.162a.217.217 0 0 1-.412 0l-.387-1.162A1.734 1.734 0 0 0 9.31 6.593l-1.162-.387a.217.217 0 0 1 0-.412l1.162-.387a1.734 1.734 0 0 0 1.097-1.097l.387-1.162zM13.863.099a.145.145 0 0 1 .274 0l.258.774c.115.346.386.617.732.732l.774.258a.145.145 0 0 1 0 .274l-.774.258a1.156 1.156 0 0 0-.732.732l-.258.774a.145.145 0 0 1-.274 0l-.258-.774a1.156 1.156 0 0 0-.732-.732l-.774-.258a.145.145 0 0 1 0-.274l.774-.258c.346-.115.617-.386.732-.732L13.863.1z"></path></symbol><symbol id="sun-fill" viewbox="0 0 16 16"><path d="M8 12a4 4 0 1 0 0-8 4 4 0 0 0 0 8zM8 0a.5.5 0 0 1 .5.5v2a.5.5 0 0 1-1 0v-2A.5.5 0 0 1 8 0zm0 13a.5.5 0 0 1 .5.5v2a.5.5 0 0 1-1 0v-2A.5.5 0 0 1 8 13zm8-5a.5.5 0 0 1-.5.5h-2a.5.5 0 0 1 0-1h2a.5.5 0 0 1 .5.5zM3 8a.5.5 0 0 1-.5.5h-2a.5.5 0 0 1 0-1h2A.5.5 0 0 1 3 8zm10.657-5.657a.5.5 0 0 1 0 .707l-1.414 1.415a.5.5 0 1 1-.707-.708l1.414-1.414a.5.5 0 0 1 .707 0zm-9.193 9.193a.5.5 0 0 1 0 .707L3.05 13.657a.5.5 0 0 1-.707-.707l1.414-1.414a.5.5 0 0 1 .707 0zm9.193 2.121a.5.5 0 0 1-.707 0l-1.414-1.414a.5.5 0 0 1 .707-.707l1.414 1.414a.5.5 0 0 1 0 .707zM4.464 4.465a.5.5 0 0 1-.707 0L2.343 3.05a.5.5 0 1 1 .707-.707l1.414 1.414a.5.5 0 0 1 0 .708z"></path></symbol></svg><a class="visually-hidden-focusable skip-link" href="#main-content">Skip to main content</a>
<div class="container-fluid top-nav-container">
<div class="col-md-8">
<div class="large-logo">
<img id="swc-logo" alt="Software Carpentry" src="assets/images/software-logo.svg"></div>
</div>
<div class="selector-container">
<div id="theme-selector">
<li class="nav-item dropdown" id="theme-button-list">
<button class="btn btn-link nav-link px-0 px-lg-2 dropdown-toggle d-flex align-items-center" id="bd-theme" type="button" aria-expanded="false" data-bs-toggle="dropdown" data-bs-display="static" aria-label="Toggle theme (auto)">
<svg class="bi my-1 theme-icon-active"><use href="#circle-half"></use></svg><i data-feather="chevron-down"></i>
</button>
<ul class="dropdown-menu dropdown-menu-end" aria-labelledby="bd-theme-text"><li>
<button type="button" class="btn dropdown-item d-flex align-items-center" data-bs-theme-value="light" aria-pressed="false">
<svg class="bi me-2 theme-icon"><use href="#sun-fill"></use></svg>
Light
<svg class="bi ms-auto d-none"><use href="#check2"></use></svg></button>
</li>
<li>
<button type="button" class="btn dropdown-item d-flex align-items-center" data-bs-theme-value="dark" aria-pressed="false">
<svg class="bi me-2 theme-icon"><use href="#moon-stars-fill"></use></svg>
Dark
<svg class="bi ms-auto d-none"><use href="#check2"></use></svg></button>
</li>
<li>
<button type="button" class="btn dropdown-item d-flex align-items-center active" data-bs-theme-value="auto" aria-pressed="true">
<svg class="bi me-2 theme-icon"><use href="#circle-half"></use></svg>
Auto
<svg class="bi ms-auto d-none"><use href="#check2"></use></svg></button>
</li>
</ul></li>
</div>
<div class="dropdown" id="instructor-dropdown">
<button class="btn btn-secondary dropdown-toggle bordered-button" type="button" id="dropdownMenu1" data-bs-toggle="dropdown" aria-expanded="false">
<i aria-hidden="true" class="icon" data-feather="eye"></i> Learner View <i data-feather="chevron-down"></i>
</button>
<ul class="dropdown-menu" aria-labelledby="dropdownMenu1"><li><button class="dropdown-item" type="button" onclick="window.location.href='instructor/05-loop.html';">Instructor View</button></li>
</ul></div>
</div>
</div>
<hr></header><nav class="navbar navbar-expand-xl bottom-nav software" aria-label="Main Navigation"><div class="container-fluid nav-container">
<button class="navbar-toggler" type="button" data-bs-toggle="collapse" data-bs-target="#navbarSupportedContent" aria-controls="navbarSupportedContent" aria-expanded="false" aria-label="Toggle Navigation">
<span class="navbar-toggler-icon"></span>
<span class="menu-title">Menu</span>
</button>
<div class="nav-logo">
<img class="small-logo" alt="Software Carpentry" src="assets/images/software-logo-sm.svg"></div>
<div class="lesson-title-md">
The Unix Shell
</div>
<div class="search-icon-sm">
<!-- TODO: do not show until we have search
<i role="img" aria-label="Search the All In One page" data-feather="search"></i>
-->
</div>
<div class="desktop-nav">
<ul class="navbar-nav me-auto mb-2 mb-lg-0"><li class="nav-item">
<span class="lesson-title">
The Unix Shell
</span>
</li>
<li class="nav-item">
<a class="nav-link" href="key-points.html">Key Points</a>
</li>
<li class="nav-item">
<a class="nav-link" href="reference.html#glossary">Glossary</a>
</li>
<li class="nav-item">
<a class="nav-link" href="profiles.html">Learner Profiles</a>
</li>
<li class="nav-item dropdown">
<button class="nav-link dropdown-toggle" id="navbarDropdown" data-bs-toggle="dropdown" aria-expanded="false">
More <i data-feather="chevron-down"></i>
</button>
<ul class="dropdown-menu" aria-labelledby="navbarDropdown"><li><a class="dropdown-item" href="discuss.html">Discussion</a></li><li><a class="dropdown-item" href="reference.html">Summary of Basic Commands</a></li>
</ul></li>
</ul></div>
<!--
<form class="d-flex col-md-2 search-form">
<fieldset disabled>
<input class="form-control me-2 searchbox" type="search" placeholder="" aria-label="">
<button class="btn btn-outline-success tablet-search-button" type="submit">
<i class="search-icon" data-feather="search" role="img" aria-label="Search the All In One page"></i>
</button>
</fieldset>
</form>
-->
<a id="search-button" class="btn btn-primary" href="aio.html" role="button" aria-label="Search the All In One page">Search the All In One page</a>
</div><!--/div.container-fluid -->
</nav><div class="col-md-12 mobile-title">
The Unix Shell
</div>
<aside class="col-md-12 lesson-progress"><div style="width: 48%" class="percentage">
48%
</div>
<div class="progress software">
<div class="progress-bar software" role="progressbar" style="width: 48%" aria-valuenow="48" aria-label="Lesson Progress" aria-valuemin="0" aria-valuemax="100">
</div>
</div>
</aside><div class="container">
<div class="row">
<!-- START: inst/pkgdown/templates/navbar.html -->
<div id="sidebar-col" class="col-lg-4">
<div id="sidebar" class="sidebar">
<nav aria-labelledby="flush-headingEleven"><button role="button" aria-label="close menu" alt="close menu" aria-expanded="true" aria-controls="sidebar" class="collapse-toggle" data-collapse="Collapse " data-episodes="Episodes ">
<i class="search-icon" data-feather="x" role="img"></i>
</button>
<div class="sidebar-inner">
<div class="row mobile-row" id="theme-row-mobile">
<div class="col" id="theme-selector">
<li class="nav-item dropdown" id="theme-button-list">
<button class="btn btn-link nav-link px-0 px-lg-2 dropdown-toggle d-flex align-items-center" id="bd-theme" type="button" aria-expanded="false" data-bs-toggle="dropdown" data-bs-display="static" aria-label="Toggle theme (auto)">
<svg class="bi my-1 theme-icon-active"><use href="#circle-half"></use></svg><span class="d-lg-none ms-1" id="bd-theme-text">Toggle Theme</span>
</button>
<ul class="dropdown-menu dropdown-menu-right" aria-labelledby="bd-theme-text"><li>
<button type="button" class="btn dropdown-item d-flex align-items-center" data-bs-theme-value="light" aria-pressed="false">
<svg class="bi me-2 theme-icon"><use href="#sun-fill"></use></svg>
Light
<svg class="bi ms-auto d-none"><use href="#check2"></use></svg></button>
</li>
<li>
<button type="button" class="btn dropdown-item d-flex align-items-center" data-bs-theme-value="dark" aria-pressed="false">
<svg class="bi me-2 theme-icon"><use href="#moon-stars-fill"></use></svg>
Dark
<svg class="bi ms-auto d-none"><use href="#check2"></use></svg></button>
</li>
<li>
<button type="button" class="btn dropdown-item d-flex align-items-center active" data-bs-theme-value="auto" aria-pressed="true">
<svg class="bi me-2 theme-icon"><use href="#circle-half"></use></svg>
Auto
<svg class="bi ms-auto d-none"><use href="#check2"></use></svg></button>
</li>
</ul></li>
</div>
</div>
<div class="row mobile-row">
<div class="col">
<div class="sidenav-view-selector">
<div class="accordion accordion-flush" id="accordionFlush9">
<div class="accordion-item">
<h2 class="accordion-header" id="flush-headingNine">
<button class="accordion-button collapsed" id="instructor" type="button" data-bs-toggle="collapse" data-bs-target="#flush-collapseNine" aria-expanded="false" aria-controls="flush-collapseNine">
<i id="eye" aria-hidden="true" class="icon" data-feather="eye"></i> Learner View
</button>
</h2>
<div id="flush-collapseNine" class="accordion-collapse collapse" aria-labelledby="flush-headingNine" data-bs-parent="#accordionFlush2">
<div class="accordion-body">
<a href="instructor/05-loop.html">Instructor View</a>
</div>
</div>
</div><!--/div.accordion-item-->
</div><!--/div.accordion-flush-->
</div><!--div.sidenav-view-selector -->
</div><!--/div.col -->
<hr></div><!--/div.mobile-row -->
<div class="accordion accordion-flush" id="accordionFlush11">
<div class="accordion-item">
<button id="chapters" class="accordion-button show" type="button" data-bs-toggle="collapse" data-bs-target="#flush-collapseEleven" aria-expanded="false" aria-controls="flush-collapseEleven">
<h2 class="accordion-header chapters" id="flush-headingEleven">
EPISODES
</h2>
</button>
<div id="flush-collapseEleven" class="accordion-collapse show collapse" aria-labelledby="flush-headingEleven" data-bs-parent="#accordionFlush11">
<div class="accordion-body">
<div class="accordion accordion-flush" id="accordionFlush1">
<div class="accordion-item">
<div class="accordion-header" id="flush-heading1">
<a href="index.html">Summary and Setup</a>
</div><!--/div.accordion-header-->
</div><!--/div.accordion-item-->
</div><!--/div.accordion-flush-->
<div class="accordion accordion-flush" id="accordionFlush2">
<div class="accordion-item">
<div class="accordion-header" id="flush-heading2">
<a href="01-intro.html">1. Introducing the Shell</a>
</div><!--/div.accordion-header-->
</div><!--/div.accordion-item-->
</div><!--/div.accordion-flush-->
<div class="accordion accordion-flush" id="accordionFlush3">
<div class="accordion-item">
<div class="accordion-header" id="flush-heading3">
<a href="02-filedir.html">2. Navigating Files and Directories</a>
</div><!--/div.accordion-header-->
</div><!--/div.accordion-item-->
</div><!--/div.accordion-flush-->
<div class="accordion accordion-flush" id="accordionFlush4">
<div class="accordion-item">
<div class="accordion-header" id="flush-heading4">
<a href="03-create.html">3. Working With Files and Directories</a>
</div><!--/div.accordion-header-->
</div><!--/div.accordion-item-->
</div><!--/div.accordion-flush-->
<div class="accordion accordion-flush" id="accordionFlush5">
<div class="accordion-item">
<div class="accordion-header" id="flush-heading5">
<a href="04-pipefilter.html">4. Pipes and Filters</a>
</div><!--/div.accordion-header-->
</div><!--/div.accordion-item-->
</div><!--/div.accordion-flush-->
<div class="accordion accordion-flush" id="accordionFlushcurrent">
<div class="accordion-item">
<div class="accordion-header" id="flush-headingcurrent">
<button class="accordion-button" type="button" data-bs-toggle="collapse" data-bs-target="#flush-collapsecurrent" aria-expanded="true" aria-controls="flush-collapsecurrent">
<span class="visually-hidden">Current Chapter</span>
<span class="current-chapter">
5. Loops
</span>
</button>
</div><!--/div.accordion-header-->
<div id="flush-collapsecurrent" class="accordion-collapse collapse show" aria-labelledby="flush-headingcurrent" data-bs-parent="#accordionFlushcurrent">
<div class="accordion-body">
<ul><li><a href="#nelles-pipeline-processing-files">Nelle’s Pipeline: Processing Files</a></li>
</ul></div><!--/div.accordion-body-->
</div><!--/div.accordion-collapse-->
</div><!--/div.accordion-item-->
</div><!--/div.accordion-flush-->
<div class="accordion accordion-flush" id="accordionFlush7">
<div class="accordion-item">
<div class="accordion-header" id="flush-heading7">
<a href="06-script.html">6. Shell Scripts</a>
</div><!--/div.accordion-header-->
</div><!--/div.accordion-item-->
</div><!--/div.accordion-flush-->
<div class="accordion accordion-flush" id="accordionFlush8">
<div class="accordion-item">
<div class="accordion-header" id="flush-heading8">
<a href="07-find.html">7. Finding Things</a>
</div><!--/div.accordion-header-->
</div><!--/div.accordion-item-->
</div><!--/div.accordion-flush-->
</div>
</div>
</div>
<hr class="half-width"><div class="accordion accordion-flush lesson-resources" id="accordionFlush12">
<div class="accordion-item">
<h2 class="accordion-header" id="flush-headingTwelve">
<button class="accordion-button collapsed" id="lesson-resources" type="button" data-bs-toggle="collapse" data-bs-target="#flush-collapseTwelve" aria-expanded="false" aria-controls="flush-collapseTwelve">
RESOURCES
</button>
</h2>
<div id="flush-collapseTwelve" class="accordion-collapse collapse" aria-labelledby="flush-headingTwelve" data-bs-parent="#accordionFlush12">
<div class="accordion-body">
<ul><li>
<a href="key-points.html">Key Points</a>
</li>
<li>
<a href="reference.html#glossary">Glossary</a>
</li>
<li>
<a href="profiles.html">Learner Profiles</a>
</li>
<li><a href="discuss.html">Discussion</a></li><li><a href="reference.html">Summary of Basic Commands</a></li>
</ul></div>
</div>
</div>
</div>
<hr class="half-width lesson-resources"><a href="aio.html">See all in one page</a>
<hr class="d-none d-sm-block d-md-none"><div class="d-grid gap-1">
</div>
</div><!-- /div.accordion -->
</div><!-- /div.sidebar-inner -->
</nav></div><!-- /div.sidebar -->
</div><!-- /div.sidebar-col -->
<!-- END: inst/pkgdown/templates/navbar.html-->
<!-- START: inst/pkgdown/templates/content-instructor.html -->
<div class="col-xl-8 col-lg-12 primary-content">
<nav class="lesson-content mx-md-4" aria-label="Previous and Next Chapter"><!-- content for small screens --><div class="d-block d-sm-block d-md-none">
<a class="chapter-link" href="04-pipefilter.html"><i aria-hidden="true" class="small-arrow" data-feather="arrow-left"></i>Previous</a>
<a class="chapter-link float-end" href="06-script.html">Next<i aria-hidden="true" class="small-arrow" data-feather="arrow-right"></i></a>
</div>
<!-- content for large screens -->
<div class="d-none d-sm-none d-md-block">
<a class="chapter-link" href="04-pipefilter.html" rel="prev">
<i aria-hidden="true" class="small-arrow" data-feather="arrow-left"></i>
Previous: Pipes and Filters
</a>
<a class="chapter-link float-end" href="06-script.html" rel="next">
Next: Shell Scripts...
<i aria-hidden="true" class="small-arrow" data-feather="arrow-right"></i>
</a>
</div>
<hr></nav><main id="main-content" class="main-content"><div class="container lesson-content">
<h1>Loops</h1>
<p>Last updated on 2023-11-10 |
<a href="https://github.com/swcarpentry/shell-novice/edit/main/episodes/05-loop.md" class="external-link">Edit this page <i aria-hidden="true" data-feather="edit"></i></a></p>
<div class="text-end">
<button role="button" aria-pressed="false" tabindex="0" id="expand-code" class="pull-right" data-expand="Expand All Solutions " data-collapse="Collapse All Solutions "> Expand All Solutions <i aria-hidden="true" data-feather="plus"></i></button>
</div>
<div class="overview card">
<h2 class="card-header">Overview</h2>
<div class="row g-0">
<div class="col-md-4">
<div class="card-body">
<div class="inner">
<h3 class="card-title">Questions</h3>
<ul><li>How can I perform the same actions on many different files?</li>
</ul></div>
</div>
</div>
<div class="col-md-8">
<div class="card-body">
<div class="inner bordered">
<h3 class="card-title">Objectives</h3>
<ul><li>Write a loop that applies one or more commands separately to each
file in a set of files.</li>
<li>Trace the values taken on by a loop variable during execution of the
loop.</li>
<li>Explain the difference between a variable’s name and its value.</li>
<li>Explain why spaces and some punctuation characters shouldn’t be used
in file names.</li>
<li>Demonstrate how to see what commands have recently been
executed.</li>
<li>Re-run recently executed commands without retyping them.</li>
</ul></div>
</div>
</div>
</div>
</div>
<p><strong>Loops</strong> are a programming construct which allow us to
repeat a command or set of commands for each item in a list. As such
they are key to productivity improvements through automation. Similar to
wildcards and tab completion, using loops also reduces the amount of
typing required (and hence reduces the number of typing mistakes).</p>
<p>Suppose we have several hundred genome data files named
<code>basilisk.dat</code>, <code>minotaur.dat</code>, and
<code>unicorn.dat</code>. For this example, we’ll use the
<code>exercise-data/creatures</code> directory which only has three
example files, but the principles can be applied to many many more files
at once.</p>
<p>The structure of these files is the same: the common name,
classification, and updated date are presented on the first three lines,
with DNA sequences on the following lines. Let’s look at the files:</p>
<div class="codewrapper sourceCode" id="cb1">
<h3 class="code-label">BASH<i aria-hidden="true" data-feather="chevron-left"></i><i aria-hidden="true" data-feather="chevron-right"></i>
</h3>
<pre class="sourceCode bash" tabindex="0"><code class="sourceCode bash"><span id="cb1-1"><a href="#cb1-1" tabindex="-1"></a><span class="ex">$</span> head <span class="at">-n</span> 5 basilisk.dat minotaur.dat unicorn.dat</span></code></pre>
</div>
<p>We would like to print out the classification for each species, which
is given on the second line of each file. For each file, we would need
to execute the command <code>head -n 2</code> and pipe this to
<code>tail -n 1</code>. We’ll use a loop to solve this problem, but
first let’s look at the general form of a loop, using the pseudo-code
below:</p>
<div class="codewrapper sourceCode" id="cb2">
<h3 class="code-label">BASH<i aria-hidden="true" data-feather="chevron-left"></i><i aria-hidden="true" data-feather="chevron-right"></i>
</h3>
<pre class="sourceCode bash" tabindex="0"><code class="sourceCode bash"><span id="cb2-1"><a href="#cb2-1" tabindex="-1"></a><span class="co"># The word "for" indicates the start of a "For-loop" command</span></span>
<span id="cb2-2"><a href="#cb2-2" tabindex="-1"></a><span class="cf">for</span> thing <span class="kw">in</span> list_of_things </span>
<span id="cb2-3"><a href="#cb2-3" tabindex="-1"></a><span class="co">#The word "do" indicates the start of job execution list</span></span>
<span id="cb2-4"><a href="#cb2-4" tabindex="-1"></a><span class="cf">do</span> </span>
<span id="cb2-5"><a href="#cb2-5" tabindex="-1"></a> <span class="co"># Indentation within the loop is not required, but aids legibility</span></span>
<span id="cb2-6"><a href="#cb2-6" tabindex="-1"></a> <span class="ex">operation_using/command</span> <span class="va">$thing</span> </span>
<span id="cb2-7"><a href="#cb2-7" tabindex="-1"></a><span class="co"># The word "done" indicates the end of a loop</span></span>
<span id="cb2-8"><a href="#cb2-8" tabindex="-1"></a><span class="cf">done</span> </span></code></pre>
</div>
<p>and we can apply this to our example like this:</p>
<div class="codewrapper sourceCode" id="cb3">
<h3 class="code-label">BASH<i aria-hidden="true" data-feather="chevron-left"></i><i aria-hidden="true" data-feather="chevron-right"></i>
</h3>
<pre class="sourceCode bash" tabindex="0"><code class="sourceCode bash"><span id="cb3-1"><a href="#cb3-1" tabindex="-1"></a><span class="ex">$</span> for filename in basilisk.dat minotaur.dat unicorn.dat</span>
<span id="cb3-2"><a href="#cb3-2" tabindex="-1"></a><span class="op">></span> do</span>
<span id="cb3-3"><a href="#cb3-3" tabindex="-1"></a><span class="op">></span> echo <span class="va">$filename</span></span>
<span id="cb3-4"><a href="#cb3-4" tabindex="-1"></a><span class="op">></span> head <span class="ex">-n</span> 2 <span class="va">$filename</span> <span class="kw">|</span> <span class="fu">tail</span> <span class="at">-n</span> 1</span>
<span id="cb3-5"><a href="#cb3-5" tabindex="-1"></a><span class="op">></span> done</span></code></pre>
</div>
<div class="codewrapper">
<h3 class="code-label">OUTPUT<i aria-hidden="true" data-feather="chevron-left"></i><i aria-hidden="true" data-feather="chevron-right"></i>
</h3>
<pre class="output" tabindex="0"><code>basilisk.dat
CLASSIFICATION: basiliscus vulgaris
minotaur.dat
CLASSIFICATION: bos hominus
unicorn.dat
CLASSIFICATION: equus monoceros</code></pre>
</div>
<div id="follow-the-prompt" class="callout">
<div class="callout-square">
<i class="callout-icon" data-feather="bell"></i>
</div>
<div id="follow-the-prompt" class="callout-inner">
<h3 class="callout-title">Follow the Prompt</h3>
<div class="callout-content">
<p>The shell prompt changes from <code>$</code> to <code>></code> and
back again as we were typing in our loop. The second prompt,
<code>></code>, is different to remind us that we haven’t finished
typing a complete command yet. A semicolon, <code>;</code>, can be used
to separate two commands written on a single line.</p>
</div>
</div>
</div>
<p>When the shell sees the keyword <code>for</code>, it knows to repeat
a command (or group of commands) once for each item in a list. Each time
the loop runs (called an iteration), an item in the list is assigned in
sequence to the <strong>variable</strong>, and the commands inside the
loop are executed, before moving on to the next item in the list. Inside
the loop, we call for the variable’s value by putting <code>$</code> in
front of it. The <code>$</code> tells the shell interpreter to treat the
variable as a variable name and substitute its value in its place,
rather than treat it as text or an external command.</p>
<p>In this example, the list is three filenames:
<code>basilisk.dat</code>, <code>minotaur.dat</code>, and
<code>unicorn.dat</code>. Each time the loop iterates, we first use
<code>echo</code> to print the value that the variable
<code>$filename</code> currently holds. This is not necessary for the
result, but beneficial for us here to have an easier time to follow
along. Next, we will run the <code>head</code> command on the file
currently referred to by <code>$filename</code>. The first time through
the loop, <code>$filename</code> is <code>basilisk.dat</code>. The
interpreter runs the command <code>head</code> on
<code>basilisk.dat</code> and pipes the first two lines to the
<code>tail</code> command, which then prints the second line of
<code>basilisk.dat</code>. For the second iteration,
<code>$filename</code> becomes <code>minotaur.dat</code>. This time, the
shell runs <code>head</code> on <code>minotaur.dat</code> and pipes the
first two lines to the <code>tail</code> command, which then prints the
second line of <code>minotaur.dat</code>. For the third iteration,
<code>$filename</code> becomes <code>unicorn.dat</code>, so the shell
runs the <code>head</code> command on that file, and <code>tail</code>
on the output of that. Since the list was only three items, the shell
exits the <code>for</code> loop.</p>
<div id="same-symbols-different-meanings" class="callout">
<div class="callout-square">
<i class="callout-icon" data-feather="bell"></i>
</div>
<div id="same-symbols-different-meanings" class="callout-inner">
<h3 class="callout-title">Same Symbols, Different Meanings</h3>
<div class="callout-content">
<p>Here we see <code>></code> being used as a shell prompt, whereas
<code>></code> is also used to redirect output. Similarly,
<code>$</code> is used as a shell prompt, but, as we saw earlier, it is
also used to ask the shell to get the value of a variable.</p>
<p>If the <em>shell</em> prints <code>></code> or <code>$</code> then
it expects you to type something, and the symbol is a prompt.</p>
<p>If <em>you</em> type <code>></code> or <code>$</code> yourself, it
is an instruction from you that the shell should redirect output or get
the value of a variable.</p>
</div>
</div>
</div>
<p>When using variables it is also possible to put the names into curly
braces to clearly delimit the variable name: <code>$filename</code> is
equivalent to <code>${filename}</code>, but is different from
<code>${file}name</code>. You may find this notation in other people’s
programs.</p>
<p>We have called the variable in this loop <code>filename</code> in
order to make its purpose clearer to human readers. The shell itself
doesn’t care what the variable is called; if we wrote this loop as:</p>
<div class="codewrapper sourceCode" id="cb5">
<h3 class="code-label">BASH<i aria-hidden="true" data-feather="chevron-left"></i><i aria-hidden="true" data-feather="chevron-right"></i>
</h3>
<pre class="sourceCode bash" tabindex="0"><code class="sourceCode bash"><span id="cb5-1"><a href="#cb5-1" tabindex="-1"></a><span class="ex">$</span> for x in basilisk.dat minotaur.dat unicorn.dat</span>
<span id="cb5-2"><a href="#cb5-2" tabindex="-1"></a><span class="op">></span> do</span>
<span id="cb5-3"><a href="#cb5-3" tabindex="-1"></a><span class="op">></span> head <span class="ex">-n</span> 2 <span class="va">$x</span> <span class="kw">|</span> <span class="fu">tail</span> <span class="at">-n</span> 1</span>
<span id="cb5-4"><a href="#cb5-4" tabindex="-1"></a><span class="op">></span> done</span></code></pre>
</div>
<p>or:</p>
<div class="codewrapper sourceCode" id="cb6">
<h3 class="code-label">BASH<i aria-hidden="true" data-feather="chevron-left"></i><i aria-hidden="true" data-feather="chevron-right"></i>
</h3>
<pre class="sourceCode bash" tabindex="0"><code class="sourceCode bash"><span id="cb6-1"><a href="#cb6-1" tabindex="-1"></a><span class="ex">$</span> for temperature in basilisk.dat minotaur.dat unicorn.dat</span>
<span id="cb6-2"><a href="#cb6-2" tabindex="-1"></a><span class="op">></span> do</span>
<span id="cb6-3"><a href="#cb6-3" tabindex="-1"></a><span class="op">></span> head <span class="ex">-n</span> 2 <span class="va">$temperature</span> <span class="kw">|</span> <span class="fu">tail</span> <span class="at">-n</span> 1</span>
<span id="cb6-4"><a href="#cb6-4" tabindex="-1"></a><span class="op">></span> done</span></code></pre>
</div>
<p>it would work exactly the same way. <em>Don’t do this.</em> Programs
are only useful if people can understand them, so meaningless names
(like <code>x</code>) or misleading names (like
<code>temperature</code>) increase the odds that the program won’t do
what its readers think it does.</p>
<p>In the above examples, the variables (<code>thing</code>,
<code>filename</code>, <code>x</code> and <code>temperature</code>)
could have been given any other name, as long as it is meaningful to
both the person writing the code and the person reading it.</p>
<p>Note also that loops can be used for other things than filenames,
like a list of numbers or a subset of data.</p>
<div id="write-your-own-loop" class="callout challenge">
<div class="callout-square">
<i class="callout-icon" data-feather="zap"></i>
</div>
<div id="write-your-own-loop" class="callout-inner">
<h3 class="callout-title">Write your own loop</h3>
<div class="callout-content">
<p>How would you write a loop that echoes all 10 numbers from 0 to
9?</p>
</div>
</div>
</div>
<div id="accordionSolution1" class="accordion challenge-accordion accordion-flush">
<div class="accordion-item">
<button class="accordion-button solution-button collapsed" type="button" data-bs-toggle="collapse" data-bs-target="#collapseSolution1" aria-expanded="false" aria-controls="collapseSolution1">
<h4 class="accordion-header" id="headingSolution1"> Show me the solution </h4>
</button>
<div id="collapseSolution1" class="accordion-collapse collapse" aria-labelledby="headingSolution1" data-bs-parent="#accordionSolution1">
<div class="accordion-body">
<div class="codewrapper sourceCode" id="cb7">
<h3 class="code-label">BASH<i aria-hidden="true" data-feather="chevron-left"></i><i aria-hidden="true" data-feather="chevron-right"></i>
</h3>
<pre class="sourceCode bash" tabindex="0"><code class="sourceCode bash"><span id="cb7-1"><a href="#cb7-1" tabindex="-1"></a><span class="ex">$</span> for loop_variable in 0 1 2 3 4 5 6 7 8 9</span>
<span id="cb7-2"><a href="#cb7-2" tabindex="-1"></a><span class="op">></span> do</span>
<span id="cb7-3"><a href="#cb7-3" tabindex="-1"></a><span class="op">></span> echo <span class="va">$loop_variable</span></span>
<span id="cb7-4"><a href="#cb7-4" tabindex="-1"></a><span class="op">></span> done</span></code></pre>
</div>
<div class="codewrapper">
<h3 class="code-label">OUTPUT<i aria-hidden="true" data-feather="chevron-left"></i><i aria-hidden="true" data-feather="chevron-right"></i>
</h3>
<pre class="output" tabindex="0"><code><span><span class="fl">0</span></span>
<span><span class="fl">1</span></span>
<span><span class="fl">2</span></span>
<span><span class="fl">3</span></span>
<span><span class="fl">4</span></span>
<span><span class="fl">5</span></span>
<span><span class="fl">6</span></span>
<span><span class="fl">7</span></span>
<span><span class="fl">8</span></span>
<span><span class="fl">9</span></span></code></pre>
</div>
</div>
</div>
</div>
</div>
<div id="variables-in-loops" class="callout challenge">
<div class="callout-square">
<i class="callout-icon" data-feather="zap"></i>
</div>
<div id="variables-in-loops" class="callout-inner">
<h3 class="callout-title">Variables in Loops</h3>
<div class="callout-content">
<p>This exercise refers to the
<code>shell-lesson-data/exercise-data/alkanes</code> directory.
<code>ls *.pdb</code> gives the following output:</p>
<div class="codewrapper">
<h3 class="code-label">OUTPUT<i aria-hidden="true" data-feather="chevron-left"></i><i aria-hidden="true" data-feather="chevron-right"></i>
</h3>
<pre class="output" tabindex="0"><code>cubane.pdb ethane.pdb methane.pdb octane.pdb pentane.pdb propane.pdb</code></pre>
</div>
<p>What is the output of the following code?</p>
<div class="codewrapper sourceCode" id="cb10">
<h3 class="code-label">BASH<i aria-hidden="true" data-feather="chevron-left"></i><i aria-hidden="true" data-feather="chevron-right"></i>
</h3>
<pre class="sourceCode bash" tabindex="0"><code class="sourceCode bash"><span id="cb10-1"><a href="#cb10-1" tabindex="-1"></a><span class="ex">$</span> for datafile in <span class="pp">*</span>.pdb</span>
<span id="cb10-2"><a href="#cb10-2" tabindex="-1"></a><span class="op">></span> do</span>
<span id="cb10-3"><a href="#cb10-3" tabindex="-1"></a><span class="op">></span> ls <span class="ex">*.pdb</span></span>
<span id="cb10-4"><a href="#cb10-4" tabindex="-1"></a><span class="op">></span> done</span></code></pre>
</div>
<p>Now, what is the output of the following code?</p>
<div class="codewrapper sourceCode" id="cb11">
<h3 class="code-label">BASH<i aria-hidden="true" data-feather="chevron-left"></i><i aria-hidden="true" data-feather="chevron-right"></i>
</h3>
<pre class="sourceCode bash" tabindex="0"><code class="sourceCode bash"><span id="cb11-1"><a href="#cb11-1" tabindex="-1"></a><span class="ex">$</span> for datafile in <span class="pp">*</span>.pdb</span>
<span id="cb11-2"><a href="#cb11-2" tabindex="-1"></a><span class="op">></span> do</span>
<span id="cb11-3"><a href="#cb11-3" tabindex="-1"></a><span class="op">></span> ls <span class="va">$datafile</span></span>
<span id="cb11-4"><a href="#cb11-4" tabindex="-1"></a><span class="op">></span> done</span></code></pre>
</div>
<p>Why do these two loops give different outputs?</p>
</div>
</div>
</div>
<div id="accordionSolution2" class="accordion challenge-accordion accordion-flush">
<div class="accordion-item">
<button class="accordion-button solution-button collapsed" type="button" data-bs-toggle="collapse" data-bs-target="#collapseSolution2" aria-expanded="false" aria-controls="collapseSolution2">
<h4 class="accordion-header" id="headingSolution2"> Show me the solution </h4>
</button>
<div id="collapseSolution2" class="accordion-collapse collapse" aria-labelledby="headingSolution2" data-bs-parent="#accordionSolution2">
<div class="accordion-body">
<p>The first code block gives the same output on each iteration through
the loop. Bash expands the wildcard <code>*.pdb</code> within the loop
body (as well as before the loop starts) to match all files ending in
<code>.pdb</code> and then lists them using <code>ls</code>. The
expanded loop would look like this:</p>
<div class="codewrapper sourceCode" id="cb12">
<h3 class="code-label">BASH<i aria-hidden="true" data-feather="chevron-left"></i><i aria-hidden="true" data-feather="chevron-right"></i>
</h3>
<pre class="sourceCode bash" tabindex="0"><code class="sourceCode bash"><span id="cb12-1"><a href="#cb12-1" tabindex="-1"></a><span class="ex">$</span> for datafile in cubane.pdb ethane.pdb methane.pdb octane.pdb pentane.pdb propane.pdb</span>
<span id="cb12-2"><a href="#cb12-2" tabindex="-1"></a><span class="op">></span> do</span>
<span id="cb12-3"><a href="#cb12-3" tabindex="-1"></a><span class="op">></span> ls <span class="ex">cubane.pdb</span> ethane.pdb methane.pdb octane.pdb pentane.pdb propane.pdb</span>
<span id="cb12-4"><a href="#cb12-4" tabindex="-1"></a><span class="op">></span> done</span></code></pre>
</div>
<div class="codewrapper">
<h3 class="code-label">OUTPUT<i aria-hidden="true" data-feather="chevron-left"></i><i aria-hidden="true" data-feather="chevron-right"></i>
</h3>
<pre class="output" tabindex="0"><code>cubane.pdb ethane.pdb methane.pdb octane.pdb pentane.pdb propane.pdb
cubane.pdb ethane.pdb methane.pdb octane.pdb pentane.pdb propane.pdb
cubane.pdb ethane.pdb methane.pdb octane.pdb pentane.pdb propane.pdb
cubane.pdb ethane.pdb methane.pdb octane.pdb pentane.pdb propane.pdb
cubane.pdb ethane.pdb methane.pdb octane.pdb pentane.pdb propane.pdb
cubane.pdb ethane.pdb methane.pdb octane.pdb pentane.pdb propane.pdb</code></pre>
</div>
<p>The second code block lists a different file on each loop iteration.
The value of the <code>datafile</code> variable is evaluated using
<code>$datafile</code>, and then listed using <code>ls</code>.</p>
<div class="codewrapper">
<h3 class="code-label">OUTPUT<i aria-hidden="true" data-feather="chevron-left"></i><i aria-hidden="true" data-feather="chevron-right"></i>
</h3>
<pre class="output" tabindex="0"><code><span><span class="va">cubane.pdb</span></span>
<span><span class="va">ethane.pdb</span></span>
<span><span class="va">methane.pdb</span></span>
<span><span class="va">octane.pdb</span></span>
<span><span class="va">pentane.pdb</span></span>
<span><span class="va">propane.pdb</span></span></code></pre>
</div>
</div>
</div>
</div>
</div>
<div id="limiting-sets-of-files" class="callout challenge">
<div class="callout-square">
<i class="callout-icon" data-feather="zap"></i>
</div>
<div id="limiting-sets-of-files" class="callout-inner">
<h3 class="callout-title">Limiting Sets of Files</h3>
<div class="callout-content">
<p>What would be the output of running the following loop in the
<code>shell-lesson-data/exercise-data/alkanes</code> directory?</p>
<div class="codewrapper sourceCode" id="cb15">
<h3 class="code-label">BASH<i aria-hidden="true" data-feather="chevron-left"></i><i aria-hidden="true" data-feather="chevron-right"></i>
</h3>
<pre class="sourceCode bash" tabindex="0"><code class="sourceCode bash"><span id="cb15-1"><a href="#cb15-1" tabindex="-1"></a><span class="ex">$</span> for filename in c<span class="pp">*</span></span>
<span id="cb15-2"><a href="#cb15-2" tabindex="-1"></a><span class="op">></span> do</span>
<span id="cb15-3"><a href="#cb15-3" tabindex="-1"></a><span class="op">></span> ls <span class="va">$filename</span></span>
<span id="cb15-4"><a href="#cb15-4" tabindex="-1"></a><span class="op">></span> done</span></code></pre>
</div>
<ol style="list-style-type: decimal"><li>No files are listed.</li>
<li>All files are listed.</li>
<li>Only <code>cubane.pdb</code>, <code>octane.pdb</code> and
<code>pentane.pdb</code> are listed.</li>
<li>Only <code>cubane.pdb</code> is listed.</li>
</ol></div>
</div>
</div>
<div id="accordionSolution3" class="accordion challenge-accordion accordion-flush">
<div class="accordion-item">
<button class="accordion-button solution-button collapsed" type="button" data-bs-toggle="collapse" data-bs-target="#collapseSolution3" aria-expanded="false" aria-controls="collapseSolution3">
<h4 class="accordion-header" id="headingSolution3"> Show me the solution </h4>
</button>
<div id="collapseSolution3" class="accordion-collapse collapse" aria-labelledby="headingSolution3" data-bs-parent="#accordionSolution3">
<div class="accordion-body">
<p>4 is the correct answer. <code>*</code> matches zero or more
characters, so any file name starting with the letter c, followed by
zero or more other characters will be matched.</p>
</div>
</div>
</div>
</div>
<div id="limiting-sets-of-files" class="callout challenge">
<div class="callout-square">
<i class="callout-icon" data-feather="zap"></i>
</div>
<div id="limiting-sets-of-files" class="callout-inner">
<h3 class="callout-title">Limiting Sets of Files<em>
(continued)</em>
</h3>
<div class="callout-content">
<p>How would the output differ from using this command instead?</p>
<div class="codewrapper sourceCode" id="cb16">
<h3 class="code-label">BASH<i aria-hidden="true" data-feather="chevron-left"></i><i aria-hidden="true" data-feather="chevron-right"></i>
</h3>
<pre class="sourceCode bash" tabindex="0"><code class="sourceCode bash"><span id="cb16-1"><a href="#cb16-1" tabindex="-1"></a><span class="ex">$</span> for filename in <span class="pp">*</span>c<span class="pp">*</span></span>
<span id="cb16-2"><a href="#cb16-2" tabindex="-1"></a><span class="op">></span> do</span>
<span id="cb16-3"><a href="#cb16-3" tabindex="-1"></a><span class="op">></span> ls <span class="va">$filename</span></span>
<span id="cb16-4"><a href="#cb16-4" tabindex="-1"></a><span class="op">></span> done</span></code></pre>
</div>
<ol style="list-style-type: decimal"><li>The same files would be listed.</li>
<li>All the files are listed this time.</li>
<li>No files are listed this time.</li>
<li>The files <code>cubane.pdb</code> and <code>octane.pdb</code> will
be listed.</li>
<li>Only the file <code>octane.pdb</code> will be listed.</li>
</ol></div>
</div>
</div>
<div id="accordionSolution4" class="accordion challenge-accordion accordion-flush">
<div class="accordion-item">
<button class="accordion-button solution-button collapsed" type="button" data-bs-toggle="collapse" data-bs-target="#collapseSolution4" aria-expanded="false" aria-controls="collapseSolution4">
<h4 class="accordion-header" id="headingSolution4"> Show me the solution </h4>
</button>
<div id="collapseSolution4" class="accordion-collapse collapse" aria-labelledby="headingSolution4" data-bs-parent="#accordionSolution4">
<div class="accordion-body">
<p>4 is the correct answer. <code>*</code> matches zero or more
characters, so a file name with zero or more characters before a letter
c and zero or more characters after the letter c will be matched.</p>
</div>
</div>
</div>
</div>
<div id="saving-to-a-file-in-a-loop---part-one" class="callout challenge">
<div class="callout-square">
<i class="callout-icon" data-feather="zap"></i>
</div>
<div id="saving-to-a-file-in-a-loop---part-one" class="callout-inner">
<h3 class="callout-title">Saving to a File in a Loop - Part One</h3>
<div class="callout-content">
<p>In the <code>shell-lesson-data/exercise-data/alkanes</code>
directory, what is the effect of this loop?</p>
<div class="codewrapper sourceCode" id="cb17">
<h3 class="code-label">BASH<i aria-hidden="true" data-feather="chevron-left"></i><i aria-hidden="true" data-feather="chevron-right"></i>
</h3>
<pre class="sourceCode bash" tabindex="0"><code class="sourceCode bash"><span id="cb17-1"><a href="#cb17-1" tabindex="-1"></a><span class="cf">for</span> alkanes <span class="kw">in</span> <span class="pp">*</span>.pdb</span>
<span id="cb17-2"><a href="#cb17-2" tabindex="-1"></a><span class="cf">do</span></span>
<span id="cb17-3"><a href="#cb17-3" tabindex="-1"></a> <span class="bu">echo</span> <span class="va">$alkanes</span></span>
<span id="cb17-4"><a href="#cb17-4" tabindex="-1"></a> <span class="fu">cat</span> <span class="va">$alkanes</span> <span class="op">></span> alkanes.pdb</span>
<span id="cb17-5"><a href="#cb17-5" tabindex="-1"></a><span class="cf">done</span></span></code></pre>
</div>
<ol style="list-style-type: decimal"><li>Prints <code>cubane.pdb</code>, <code>ethane.pdb</code>,
<code>methane.pdb</code>, <code>octane.pdb</code>,
<code>pentane.pdb</code> and <code>propane.pdb</code>, and the text from
<code>propane.pdb</code> will be saved to a file called
<code>alkanes.pdb</code>.</li>
<li>Prints <code>cubane.pdb</code>, <code>ethane.pdb</code>, and
<code>methane.pdb</code>, and the text from all three files would be
concatenated and saved to a file called <code>alkanes.pdb</code>.</li>
<li>Prints <code>cubane.pdb</code>, <code>ethane.pdb</code>,
<code>methane.pdb</code>, <code>octane.pdb</code>, and
<code>pentane.pdb</code>, and the text from <code>propane.pdb</code>
will be saved to a file called <code>alkanes.pdb</code>.</li>
<li>None of the above.</li>
</ol></div>
</div>
</div>
<div id="accordionSolution5" class="accordion challenge-accordion accordion-flush">
<div class="accordion-item">
<button class="accordion-button solution-button collapsed" type="button" data-bs-toggle="collapse" data-bs-target="#collapseSolution5" aria-expanded="false" aria-controls="collapseSolution5">
<h4 class="accordion-header" id="headingSolution5"> Show me the solution </h4>
</button>
<div id="collapseSolution5" class="accordion-collapse collapse" aria-labelledby="headingSolution5" data-bs-parent="#accordionSolution5">
<div class="accordion-body">
<ol style="list-style-type: decimal"><li>The text from each file in turn gets written to the
<code>alkanes.pdb</code> file. However, the file gets overwritten on
each loop iteration, so the final content of <code>alkanes.pdb</code> is
the text from the <code>propane.pdb</code> file.</li>
</ol></div>
</div>
</div>
</div>
<div id="saving-to-a-file-in-a-loop---part-two" class="callout challenge">
<div class="callout-square">
<i class="callout-icon" data-feather="zap"></i>
</div>
<div id="saving-to-a-file-in-a-loop---part-two" class="callout-inner">
<h3 class="callout-title">Saving to a File in a Loop - Part Two</h3>
<div class="callout-content">
<p>Also in the <code>shell-lesson-data/exercise-data/alkanes</code>
directory, what would be the output of the following loop?</p>
<div class="codewrapper sourceCode" id="cb18">
<h3 class="code-label">BASH<i aria-hidden="true" data-feather="chevron-left"></i><i aria-hidden="true" data-feather="chevron-right"></i>
</h3>
<pre class="sourceCode bash" tabindex="0"><code class="sourceCode bash"><span id="cb18-1"><a href="#cb18-1" tabindex="-1"></a><span class="cf">for</span> datafile <span class="kw">in</span> <span class="pp">*</span>.pdb</span>
<span id="cb18-2"><a href="#cb18-2" tabindex="-1"></a><span class="cf">do</span></span>
<span id="cb18-3"><a href="#cb18-3" tabindex="-1"></a> <span class="fu">cat</span> <span class="va">$datafile</span> <span class="op">>></span> all.pdb</span>
<span id="cb18-4"><a href="#cb18-4" tabindex="-1"></a><span class="cf">done</span></span></code></pre>
</div>
<ol style="list-style-type: decimal"><li>All of the text from <code>cubane.pdb</code>,
<code>ethane.pdb</code>, <code>methane.pdb</code>,
<code>octane.pdb</code>, and <code>pentane.pdb</code> would be
concatenated and saved to a file called <code>all.pdb</code>.</li>
<li>The text from <code>ethane.pdb</code> will be saved to a file called
<code>all.pdb</code>.</li>
<li>All of the text from <code>cubane.pdb</code>,
<code>ethane.pdb</code>, <code>methane.pdb</code>,
<code>octane.pdb</code>, <code>pentane.pdb</code> and
<code>propane.pdb</code> would be concatenated and saved to a file
called <code>all.pdb</code>.</li>
<li>All of the text from <code>cubane.pdb</code>,
<code>ethane.pdb</code>, <code>methane.pdb</code>,
<code>octane.pdb</code>, <code>pentane.pdb</code> and
<code>propane.pdb</code> would be printed to the screen and saved to a
file called <code>all.pdb</code>.</li>
</ol></div>
</div>
</div>
<div id="accordionSolution6" class="accordion challenge-accordion accordion-flush">
<div class="accordion-item">
<button class="accordion-button solution-button collapsed" type="button" data-bs-toggle="collapse" data-bs-target="#collapseSolution6" aria-expanded="false" aria-controls="collapseSolution6">
<h4 class="accordion-header" id="headingSolution6"> Show me the solution </h4>
</button>
<div id="collapseSolution6" class="accordion-collapse collapse" aria-labelledby="headingSolution6" data-bs-parent="#accordionSolution6">
<div class="accordion-body">
<p>3 is the correct answer. <code>>></code> appends to a file,
rather than overwriting it with the redirected output from a command.
Given the output from the <code>cat</code> command has been redirected,
nothing is printed to the screen.</p>
</div>
</div>
</div>
</div>
<p>Let’s continue with our example in the
<code>shell-lesson-data/exercise-data/creatures</code> directory. Here’s
a slightly more complicated loop:</p>
<div class="codewrapper sourceCode" id="cb19">
<h3 class="code-label">BASH<i aria-hidden="true" data-feather="chevron-left"></i><i aria-hidden="true" data-feather="chevron-right"></i>
</h3>
<pre class="sourceCode bash" tabindex="0"><code class="sourceCode bash"><span id="cb19-1"><a href="#cb19-1" tabindex="-1"></a><span class="ex">$</span> for filename in <span class="pp">*</span>.dat</span>
<span id="cb19-2"><a href="#cb19-2" tabindex="-1"></a><span class="op">></span> do</span>
<span id="cb19-3"><a href="#cb19-3" tabindex="-1"></a><span class="op">></span> echo <span class="va">$filename</span></span>
<span id="cb19-4"><a href="#cb19-4" tabindex="-1"></a><span class="op">></span> head <span class="ex">-n</span> 100 <span class="va">$filename</span> <span class="kw">|</span> <span class="fu">tail</span> <span class="at">-n</span> 20</span>
<span id="cb19-5"><a href="#cb19-5" tabindex="-1"></a><span class="op">></span> done</span></code></pre>
</div>
<p>The shell starts by expanding <code>*.dat</code> to create the list
of files it will process. The <strong>loop body</strong> then executes
two commands for each of those files. The first command,
<code>echo</code>, prints its command-line arguments to standard output.
For example:</p>
<div class="codewrapper sourceCode" id="cb20">
<h3 class="code-label">BASH<i aria-hidden="true" data-feather="chevron-left"></i><i aria-hidden="true" data-feather="chevron-right"></i>
</h3>
<pre class="sourceCode bash" tabindex="0"><code class="sourceCode bash"><span id="cb20-1"><a href="#cb20-1" tabindex="-1"></a><span class="ex">$</span> echo hello there</span></code></pre>
</div>
<p>prints:</p>
<div class="codewrapper">
<h3 class="code-label">OUTPUT<i aria-hidden="true" data-feather="chevron-left"></i><i aria-hidden="true" data-feather="chevron-right"></i>
</h3>
<pre class="output" tabindex="0"><code>hello there</code></pre>
</div>
<p>In this case, since the shell expands <code>$filename</code> to be
the name of a file, <code>echo $filename</code> prints the name of the
file. Note that we can’t write this as:</p>
<div class="codewrapper sourceCode" id="cb22">
<h3 class="code-label">BASH<i aria-hidden="true" data-feather="chevron-left"></i><i aria-hidden="true" data-feather="chevron-right"></i>
</h3>
<pre class="sourceCode bash" tabindex="0"><code class="sourceCode bash"><span id="cb22-1"><a href="#cb22-1" tabindex="-1"></a><span class="ex">$</span> for filename in <span class="pp">*</span>.dat</span>
<span id="cb22-2"><a href="#cb22-2" tabindex="-1"></a><span class="op">></span> do</span>
<span id="cb22-3"><a href="#cb22-3" tabindex="-1"></a><span class="op">></span> <span class="va">$filename</span></span>
<span id="cb22-4"><a href="#cb22-4" tabindex="-1"></a><span class="op">></span> head <span class="ex">-n</span> 100 <span class="va">$filename</span> <span class="kw">|</span> <span class="fu">tail</span> <span class="at">-n</span> 20</span>
<span id="cb22-5"><a href="#cb22-5" tabindex="-1"></a><span class="op">></span> done</span></code></pre>
</div>
<p>because then the first time through the loop, when
<code>$filename</code> expanded to <code>basilisk.dat</code>, the shell
would try to run <code>basilisk.dat</code> as a program. Finally, the
<code>head</code> and <code>tail</code> combination selects lines 81-100
from whatever file is being processed (assuming the file has at least
100 lines).</p>
<div id="spaces-in-names" class="callout">
<div class="callout-square">
<i class="callout-icon" data-feather="bell"></i>
</div>
<div id="spaces-in-names" class="callout-inner">
<h3 class="callout-title">Spaces in Names</h3>
<div class="callout-content">
<p>Spaces are used to separate the elements of the list that we are
going to loop over. If one of those elements contains a space character,
we need to surround it with quotes, and do the same thing to our loop
variable. Suppose our data files are named:</p>
<pre class="source"><code>red dragon.dat
purple unicorn.dat</code></pre>
<p>To loop over these files, we would need to add double quotes like
so:</p>
<div class="codewrapper sourceCode" id="cb24">
<h3 class="code-label">BASH<i aria-hidden="true" data-feather="chevron-left"></i><i aria-hidden="true" data-feather="chevron-right"></i>
</h3>
<pre class="sourceCode bash" tabindex="0"><code class="sourceCode bash"><span id="cb24-1"><a href="#cb24-1" tabindex="-1"></a><span class="ex">$</span> for filename in <span class="st">"red dragon.dat"</span> <span class="st">"purple unicorn.dat"</span></span>
<span id="cb24-2"><a href="#cb24-2" tabindex="-1"></a><span class="op">></span> do</span>
<span id="cb24-3"><a href="#cb24-3" tabindex="-1"></a><span class="op">></span> head <span class="ex">-n</span> 100 <span class="st">"</span><span class="va">$filename</span><span class="st">"</span> <span class="kw">|</span> <span class="fu">tail</span> <span class="at">-n</span> 20</span>
<span id="cb24-4"><a href="#cb24-4" tabindex="-1"></a><span class="op">></span> done</span></code></pre>
</div>
<p>It is simpler to avoid using spaces (or other special characters) in
filenames.</p>
<p>The files above don’t exist, so if we run the above code, the
<code>head</code> command will be unable to find them; however, the
error message returned will show the name of the files it is
expecting:</p>
<div class="codewrapper">
<h3 class="code-label">ERROR<i aria-hidden="true" data-feather="chevron-left"></i><i aria-hidden="true" data-feather="chevron-right"></i>
</h3>
<pre class="error" tabindex="0"><code>head: cannot open ‘red dragon.dat' for reading: No such file or directory
head: cannot open ‘purple unicorn.dat' for reading: No such file or directory</code></pre>
</div>
<p>Try removing the quotes around <code>$filename</code> in the loop
above to see the effect of the quote marks on spaces. Note that we get a
result from the loop command for unicorn.dat when we run this code in
the <code>creatures</code> directory:</p>
<div class="codewrapper">
<h3 class="code-label">OUTPUT<i aria-hidden="true" data-feather="chevron-left"></i><i aria-hidden="true" data-feather="chevron-right"></i>
</h3>
<pre class="output" tabindex="0"><code>head: cannot open ‘red' for reading: No such file or directory
head: cannot open ‘dragon.dat' for reading: No such file or directory
head: cannot open ‘purple' for reading: No such file or directory
CGGTACCGAA
AAGGGTCGCG
CAAGTGTTCC
...</code></pre>
</div>
</div>
</div>
</div>
<p>We would like to modify each of the files in
<code>shell-lesson-data/exercise-data/creatures</code>, but also save a
version of the original files. We want to copy the original files to new
files named <code>original-basilisk.dat</code> and
<code>original-unicorn.dat</code>, for example. We can’t use:</p>
<div class="codewrapper sourceCode" id="cb27">
<h3 class="code-label">BASH<i aria-hidden="true" data-feather="chevron-left"></i><i aria-hidden="true" data-feather="chevron-right"></i>
</h3>
<pre class="sourceCode bash" tabindex="0"><code class="sourceCode bash"><span id="cb27-1"><a href="#cb27-1" tabindex="-1"></a><span class="ex">$</span> cp <span class="pp">*</span>.dat original-<span class="pp">*</span>.dat</span></code></pre>
</div>
<p>because that would expand to:</p>
<div class="codewrapper sourceCode" id="cb28">
<h3 class="code-label">BASH<i aria-hidden="true" data-feather="chevron-left"></i><i aria-hidden="true" data-feather="chevron-right"></i>
</h3>
<pre class="sourceCode bash" tabindex="0"><code class="sourceCode bash"><span id="cb28-1"><a href="#cb28-1" tabindex="-1"></a><span class="ex">$</span> cp basilisk.dat minotaur.dat unicorn.dat original-<span class="pp">*</span>.dat</span></code></pre>
</div>
<p>This wouldn’t back up our files, instead we get an error:</p>
<div class="codewrapper">
<h3 class="code-label">ERROR<i aria-hidden="true" data-feather="chevron-left"></i><i aria-hidden="true" data-feather="chevron-right"></i>
</h3>
<pre class="error" tabindex="0"><code>cp: target `original-*.dat' is not a directory</code></pre>
</div>
<p>This problem arises when <code>cp</code> receives more than two
inputs. When this happens, it expects the last input to be a directory
where it can copy all the files it was passed. Since there is no
directory named <code>original-*.dat</code> in the
<code>creatures</code> directory, we get an error.</p>
<p>Instead, we can use a loop:</p>
<div class="codewrapper sourceCode" id="cb30">
<h3 class="code-label">BASH<i aria-hidden="true" data-feather="chevron-left"></i><i aria-hidden="true" data-feather="chevron-right"></i>
</h3>
<pre class="sourceCode bash" tabindex="0"><code class="sourceCode bash"><span id="cb30-1"><a href="#cb30-1" tabindex="-1"></a><span class="ex">$</span> for filename in <span class="pp">*</span>.dat</span>
<span id="cb30-2"><a href="#cb30-2" tabindex="-1"></a><span class="op">></span> do</span>
<span id="cb30-3"><a href="#cb30-3" tabindex="-1"></a><span class="op">></span> cp <span class="va">$filename</span> original-<span class="va">$filename</span></span>
<span id="cb30-4"><a href="#cb30-4" tabindex="-1"></a><span class="op">></span> done</span></code></pre>
</div>
<p>This loop runs the <code>cp</code> command once for each filename.
The first time, when <code>$filename</code> expands to
<code>basilisk.dat</code>, the shell executes:</p>
<div class="codewrapper sourceCode" id="cb31">
<h3 class="code-label">BASH<i aria-hidden="true" data-feather="chevron-left"></i><i aria-hidden="true" data-feather="chevron-right"></i>
</h3>
<pre class="sourceCode bash" tabindex="0"><code class="sourceCode bash"><span id="cb31-1"><a href="#cb31-1" tabindex="-1"></a><span class="fu">cp</span> basilisk.dat original-basilisk.dat</span></code></pre>
</div>
<p>The second time, the command is:</p>
<div class="codewrapper sourceCode" id="cb32">
<h3 class="code-label">BASH<i aria-hidden="true" data-feather="chevron-left"></i><i aria-hidden="true" data-feather="chevron-right"></i>
</h3>
<pre class="sourceCode bash" tabindex="0"><code class="sourceCode bash"><span id="cb32-1"><a href="#cb32-1" tabindex="-1"></a><span class="fu">cp</span> minotaur.dat original-minotaur.dat</span></code></pre>
</div>
<p>The third and last time, the command is:</p>
<div class="codewrapper sourceCode" id="cb33">
<h3 class="code-label">BASH<i aria-hidden="true" data-feather="chevron-left"></i><i aria-hidden="true" data-feather="chevron-right"></i>
</h3>
<pre class="sourceCode bash" tabindex="0"><code class="sourceCode bash"><span id="cb33-1"><a href="#cb33-1" tabindex="-1"></a><span class="fu">cp</span> unicorn.dat original-unicorn.dat</span></code></pre>
</div>
<p>Since the <code>cp</code> command does not normally produce any
output, it’s hard to check that the loop is working correctly. However,
we learned earlier how to print strings using <code>echo</code>, and we
can modify the loop to use <code>echo</code> to print our commands
without actually executing them. As such we can check what commands
<em>would be</em> run in the unmodified loop.</p>
<p>The following diagram shows what happens when the modified loop is
executed and demonstrates how the judicious use of <code>echo</code> is
a good debugging technique.</p>
<figure><img src="fig/shell_script_for_loop_flow_chart.svg" alt='The for loop "for filename in .dat; do echo cp $filename original-$filename;done" will successively assign the names of all ".dat" files in your currentdirectory to the variable "$filename" and then execute the command. With thefiles "basilisk.dat", "minotaur.dat" and "unicorn.dat" in the current directorythe loop will successively call the echo command three times and print threelines: "cp basislisk.dat original-basilisk.dat", then "cp minotaur.datoriginal-minotaur.dat" and finally "cp unicorn.datoriginal-unicorn.dat"' class="figure mx-auto d-block"></figure><section><h2 class="section-heading" id="nelles-pipeline-processing-files">Nelle’s Pipeline: Processing Files<a class="anchor" aria-label="anchor" href="#nelles-pipeline-processing-files"></a></h2>