for runs the same lines one time for each item in a list. A variable holds the item of each turn:
bash
for day in mon tue wed; do
echo "open on $day"
donetext
open on mon
open on tue
open on wedTurn 1: day=monecho "open on mon"
Turn 2: day=tueecho "open on tue"
Turn 3: day=wedecho "open on wed"
| Word | Means |
|---|---|
for NAME in LIST; do | For each item in LIST, set NAME to it, then run the lines |
done | The end of the lines to repeat |
Where the list comes from
| List | Gives |
|---|---|
mon tue wed | The words, as you typed them |
~/logs/*.log | Each path that matches the wildcard |
"$@" | Each argument of the script |
{1..5} | 1 2 3 4 5 |
Do something with each file
bash
#!/bin/bash
for f in ~/logs/*.log; do
echo "$f: $(grep -c ERROR "$f") errors"
donetext
/home/learner/logs/door.log: 0 errors
/home/learner/logs/till.log: 3 errorsPut the variable in double quotes, "$f", so that a path with a space stays one argument.