Control Flow statement using “for” loop

In shell scripting, the “for” loop is a control flow statement that allows you to execute a block of code multiple times. The “for” loop iterates over a list of values, and on each iteration, it assigns the current value to a loop variable and executes the code block.

Here is the syntax for a “for” loop in Bash:

for VARIABLE in LIST
do
# code block to be executed
done

In this syntax, VARIABLE is the loop variable that takes on the value of each element in the LIST on each iteration. The LIST is a list of values that the loop will iterate over. The “do” keyword indicates the start of the code block to be executed, and the “done” keyword indicates the end of the code block.

Here is an example of a “for” loop in Bash:

#!/bin/bash
# Declare an array of colors
colors=("red" "green" "blue")
# Iterate over the array of colors
for color in "${colors[@]}"
do

# Print the value of the loop variable
echo "The current color is: $color"
done

This script will print the following output:

The current color is: red The current color is: green The current color is: blue

In the “for” loop, the loop variable “color” takes on the value of each element in the “colors” array on each iteration, and the code block is executed with the current value of the loop variable.

You can also use the “seq” command to generate a list of numbers for the “for” loop to iterate over. For example:

#!/bin/bash
# Generate a list of numbers from 1 to 10
for i in $(seq 1 10) do
# Print the value of the loop variable
echo "The current number is: $i"
done

This script will print the following output:

The current number is: 1
The current number is: 2
The current number is: 3

The current number is: 10

The “for” loop is a useful tool for iterating over a list of values and performing tasks on each iteration.

You can use a “for” loop in shell scripting to perform operations on a list of files. To do this, you can use the “find” command to generate a list of files that match a certain criteria, and then iterate over the list of files using a “for” loop.

Here is an example of a “for” loop that uses the “find” command to list all the files in the current directory and its subdirectories, and then prints the name of each file:

#!/bin/bash
# Use the find command to list all files in the current directory and its subdirectories
for file in $(find . -type f)
do
# Print the name of the file
echo "The current file is: $file"
done

In this example, the “find” command searches for all files (-type f) in the current directory (.) and its subdirectories, and generates a list of the files. The “for” loop then iterates over the list of files, and the loop variable “file” takes on the value of each file on each iteration. The code block is executed with the current value of the loop variable, which in this case is to print the name of the file.

You can also use the “find” command with other options to search for specific types of files or to exclude certain directories from the search.

C-style for loop in bash

In Bash, you can use a “for” loop similar to the “for” loop in C language. To do this, you can specify a range of values for the loop variable, and then use the “do” keyword to start the loop body. Here is an example of a “for” loop in Bash that is similar to a “for” loop in C:

#!/bin/bash
# Iterate over a range of numbers

for (( i=1; i<=10; i++ ))
do
# Print the value of i
echo "The current value of i is: $i"
done

In this example, the loop variable “i” is initialized to 1, and the loop condition is that the value of “i” is less than or equal to 10. The loop body is executed with the current value of “i”, and then the value of “i” is incremented by 1 on each iteration.

The output of this script would be:

The current value of i is: 1
The current value of i is: 2
...
The current value of i is: 10

Note that in this example, the loop variable “i” is defined within the loop construct.

Loop through arguments passed to the bash script

To loop through the arguments passed to a Bash script, you can use the “for” loop and the special variable “$@”, which expands to the list of all the arguments passed to the script. Here is an example of how to use a “for” loop to iterate over the arguments passed to a script:

#!/bin/bash
# Loop through the arguments
for arg in "$@"
do
# Print the current argument
echo "The current argument is: $arg"
done

In this example, the “for” loop iterates over the list of arguments passed to the script, and the loop variable “arg” is set to the current argument on each iteration. The “echo” statement is then used to print the current value of “arg”.

To run this script, you can pass it a list of arguments on the command line. For example:

./my_script.sh arg1 arg2 arg3

The output of this script would be:

The current argument is: arg1
The current argument is: arg2
The current argument is: arg3

Note that the “$@” variable expands to the list of all the arguments passed to the script, including any options or flags.

Loop through outputs of another command

To loop through the output of a command in Bash, you can use the “for” loop and the “in” keyword, followed by the command inside backticks (“). The backticks allow you to execute the command and substitute its output in place. Here is an example of how to use a “for” loop to iterate over the output of a command:

#!/bin/bash
# Loop through the output of the ls command
for file in `ls`
do
# Print the current file name
echo "The current file is: $file"
done

In this example, the “for” loop iterates over the list of files returned by the “ls” command, and the loop variable “file” is set to the current file name on each iteration. The “echo” statement is then used to print the current value of “file”.

The output of this script would be a list of the files in the current directory, one file per line:

The current file is: file1
The current file is: file2
The current file is: file3
...

Note that you can use this technique to loop through the output of any command, not just the “ls” command. Just replace the “ls” command with the command you want to execute, and the loop variable will be set to each line of output returned by the command.

continue and break to control for loops

The “continue” and “break” statements can be used to control the flow of a “for” loop in Bash. The “continue” statement causes the loop to skip the rest of the current iteration and move on to the next one, while the “break” statement causes the loop to exit completely.

Here is an example of how to use the “continue” statement in a “for” loop:

#!/bin/bash
# Loop through the numbers 1 to 10
for i in {1..10}
do
# If the current number is even, skip the rest of the iteration
if [ $((i % 2)) -eq 0 ]
then
continue
fi

# Print the current number
echo $i
done

In this example, the “for” loop iterates over the numbers 1 to 10, and the “if” statement checks if the current number is even. If the number is even, the “continue” statement is executed and the loop moves on to the next iteration. If the number is odd, the “echo” statement is executed and the number is printed.

The output of this script would be a list of the odd numbers from 1 to 10, one number per line:

1
3
5
7
9
The “break” statement in a “for” loop:
#!/bin/bash
# Loop through the numbers 1 to 10
for i in {1..10}
do
# If the current number is 5, exit the loop
if [ $i -eq 5 ] then break
fi
# Print the current number
echo $i
done

In this example, the “for” loop iterates over the numbers 1 to 10, and the “if” statement checks if the current number is 5. If the number is 5, the “break” statement is executed and the loop exits completely. If the number is not 5, the “echo” statement is executed and the number is printed.

The output of this script would be a list of the numbers 1 to 4, one number per line:

1
2
3
4

You can also use the “break” statement with a loop label to exit from a nested loop. For example:

#!/bin/bash
# Loop through the numbers 1 to 10

outer: for i in {1..10}
do
# Loop through the numbers 1 to 10
inner: for j in {1..10}
do
# If the current numbers are 5 and 6, exit both loops
if [ $i -eq 5 -a $j -eq 6 ] then
break outer

fi
# Print the current numbers
echo "$i, $j"
done
done

In this example, the “for” loops iterate over the numbers 1 to 10, and the “if” statement checks if the current numbers are 5 and 6. If they are, the “break outer” statement is executed and both loops exit completely. If the numbers are not 5 and 6, the “echo” statement is executed and the numbers are printed.

The output of this script would be a list of the numbers 1 to 4 in the outer loop and 1 to 5 in the inner loop, one pair of numbers per line, until the numbers 5 and 6 are reached:

1, 1
1, 2
1, 3
1, 4
1, 5
2, 1
2, 2
2, 3
2, 4
2, 5
3, 1
3, 2
3, 3
3, 4
3, 5
4, 1
4, 2
4, 3
4, 4
4, 5

Summary:
In shell scripting, the “for” loop is a control flow statement that allows you to execute a block of code multiple times. It iterates over a list of values and assigns the current value to a loop variable on each iteration. The “for” loop can be used to perform tasks on a list of files or to generate a list of numbers. It can also be used in a C-style format, where the loop variable is initialized, a condition is set, and the loop variable is incremented on each iteration. The “for” loop is a useful tool for iterating over a list of values and performing tasks on each iteration.

Questions for reveiw:

  1. What is the syntax for a “for” loop in Bash?
  2. What is an example of using the “seq” command with a “for” loop in Bash?
  3. How can the “find” command be used in a “for” loop in Bash?
  4. How is a C-style “for” loop used in Bash?
  5. Can a “for” loop be used to iterate over the elements of an array in Bash? If so, how?
  6. What is the purpose of the “do” and “done” keywords in a Bash “for” loop?

License

Developers ultimate guide: Linux Bash scripting Copyright © 2022 by Matin Maleki. All Rights Reserved.

Share This Book