In a shell script, a for
loop allows you to iterate over a set of values or a range of numbers. Here are a few examples of how to use for
loops in different contexts:
Example 1: Iterating Over a List of Values
Example 2: Iterating Over a Range of Numbers
Example 3: Iterating Over Files in a Directory
Example 4: Using C-Style Syntax
You can save any of these scripts to a file (e.g., script.sh
), make it executable with chmod +x script.sh
, and run it with ./script.sh
.