Using the `for` Command (with examples)
The for
command is a useful looping construct in Bash that allows you to execute a command or a set of commands multiple times. This command is particularly handy when you want to perform a specific operation on a list of items, numbers, or directories.
Using the for
command to execute commands for each specified item
The for
command can be used to execute a command or a set of commands for each of the specified items. Here is an example:
for variable in item1 item2 ...; do
echo "Loop is executed"
done
Motivation: This use case is useful when you want to perform a certain action on a fixed set of items. For example, you might want to print a message for each day of the week.
Explanation: The
variable
is a placeholder that takes the value of each item in the list sequentially. The command(s) within thedo
anddone
block will be executed for each item in the list.Example output:
Loop is executed
Loop is executed
...
Using the for
command to iterate over a range of numbers
The for
command can also be used to iterate over a given range of numbers. Here is an example:
for variable in {from..to..step}; do
echo "Loop is executed"
done
Motivation: This use case is helpful when you need to perform repetitive tasks on a range of numbers. For example, you might want to process a series of files numbered sequentially.
Explanation: The
from
parameter represents the starting value, theto
parameter represents the ending value, and thestep
parameter represents the increment between each number. The command(s) within thedo
anddone
block will be executed for each number in the range.Example output:
Loop is executed
Loop is executed
...
Using the for
command to iterate over a list of files
The for
command can be used to iterate over a given list of files. Here is an example:
for variable in path/to/file1 path/to/file2 ...; do
echo "Loop is executed"
done
Motivation: This use case is valuable when you need to perform an operation on multiple files that can be specified in a list. For example, you might want to process a set of image files.
Explanation: The
variable
takes the value of each file in the list sequentially. The command(s) within thedo
anddone
block will be executed for each file in the list.Example output:
Loop is executed
Loop is executed
...
Using the for
command to iterate over a list of directories
The for
command is also useful for iterating over a given list of directories. Here is an example:
for variable in path/to/directory1/ path/to/directory2/ ...; do
echo "Loop is executed"
done
Motivation: This use case is beneficial when you want to perform a specific operation in each directory listed. For example, you might need to search for files in multiple directories.
Explanation: The
variable
takes the value of each directory in the list sequentially. The command(s) within thedo
anddone
block will be executed for each directory in the list.Example output:
Loop is executed
Loop is executed
...
Using the for
command to perform a command in every directory
The for
command can be used to perform a given command in every directory within the current directory. Here is an example:
for variable in */; do
(cd "$variable" || continue
echo "Loop is executed")
done
Motivation: This use case is useful when you want to perform an operation in every subdirectory within the current working directory. For example, you might want to compress files in each subdirectory.
Explanation: The
variable
takes the value of each directory within the current directory sequentially. Thecd
command is used to change the working directory to a subdirectory. The(cd "$variable" || continue)
ensures that the loop continues even if there are errors encountered while changing the directory. The command(s) within thedo
anddone
block will be executed in each directory.Example output:
Loop is executed in directory1
Loop is executed in directory2
...
By utilizing different variations of the for
command, you can efficiently perform repetitive tasks in Bash. Whether it’s iterating over a list of items, numbers, or directories, the for
command provides a flexible solution for automation and productivity.