1. for over a glob
for iterates a list or glob; *.txt expands to every matching filename and binds each to f.
for f in *.txt; doTyporithm · medium
Step 1 of 8 · intro card
Scripting
for iterates a list or glob, running its body (between do and done) once per item; while read line walks a file line by line when you feed it with done < input.txt. Quote "$f" so filenames with spaces survive.
A for loop iterates a list of words, most often a glob like *.txt that the shell expands into matching filenames. Each value binds to the loop variable, and the body between do and done runs once per item.
Always quote the variable: echo "$f" handles filenames with spaces, where an unquoted $f would split into separate words.
while read line reads the input one line at a time, looping until there is nothing left. Redirecting a file in with done < input.txt feeds that file to read, so the loop processes it line by line. Loops are how scripts handle many items.
for binds each glob/list item; body sits between do and done.
Quote "$f" to survive spaces in filenames.
while read line + done < file walks a file line by line.
for f in *.txt; do
echo "$f"
done
while read line; do
echo "$line"
done < input.txtConnect each part of the code to the concept before you type.
for iterates a list or glob; *.txt expands to every matching filename and binds each to f.
for f in *.txt; doThe body runs once per item; quoting "$f" keeps filenames with spaces intact.
echo "$f"The body lives between do and done, which marks where the loop ends.
donewhile read line reads one line per iteration until input is exhausted.
while read line; doRedirecting < input.txt into done feeds the file to read, so the loop walks it line by line.
done < input.txtRead the concept map and follow the code breakdown.
Type the implementation, repair mistakes and work through every recall exercise.
Use the final check without hints; revisit weak concepts in short practice sessions.