Name

for

Synopsis

for x [in list]
do 
    commands 
done

For variable x (in optional list of values), do commands. If in list is omitted, "$@" (the positional parameters) is assumed.

Examples

Paginate files specified on the command line, and save each result:

for file; do
    pr $file > $file.tmp
done

Same, but put entire loop into the background:

for file; do
    pr $file > $file.tmp
done &

Search chapters for a list of words (like fgrep -f):

for item in `cat program_list`
do
    echo "Checking chapters for"
    echo "references to program $item…"
    grep -c "$item.[co]" chap*
done

Extract a one-word title from each file and use as new filename:

for file
do
    name=`sed -n ′s/NAME: //p′ $file`
    mv $file $name
done

Get bash Quick Reference now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.