June 2025
Beginner to intermediate
473 pages
13h 30m
English
Bash provides several commands to choose from for creating loops. In this section, I’ll start with for, before I provide a brief introduction to while and until. The basic syntax of the for loop is shown in the following listing. Note the placement of the semicolon before and not after do! You can avoid this semicolon by starting a new line with do.
for myvar in mylist; do command1 command2done# equivalentfor myvar in mylist; do command1; command2; done# Examplefor item in a b c; do echo $item; done a b c
The myvar loop variable thus runs through all values of the specified list. (You’ll see momentarily that “list” is a fairly generic term: You can use for to loop through parameters, filenames, lines of a text file, etc.) Be ...
Read now
Unlock full access