O'Reilly logo

Linux in a Nutshell, Fourth Edition by Aaron Weber, Stephen Figgins, Ellen Siever

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Name

foreach

Synopsis

                  foreach 
                  name 
                  (
                  wordlist
                  )    
                  commands
                  end
               

Assign variable name to each value in wordlist and execute commands between foreach and end. You can use foreach as a multiline command issued at the shell prompt (first of the following examples), or you can use it in a shell script (second example).

Examples

Rename all files that begin with a capital letter:

% foreach i ([A-Z]*)
? mv $i $i.new
? end

Check whether each command-line argument is an option or not:

                  foreach arg ($argv)
                     # does it begin with - ?
                     if ("$arg" =~ -*) then
                        echo "Argument is an option"
                     else
                        echo "Argument is a filename"
                     endif
                  end

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required