Skip to content
Home » Mastering Bash for Loop with Expressions

Mastering Bash for Loop with Expressions

The Bash for loop is a powerful command-line tool that allows you to execute a command or series of commands for a specified number of times. It is a staple of shell scripting and can be used for tasks ranging from simple repetition to complex automation.

Requirements and Dependencies

To use the Bash for loop, you will need a Unix-based operating system such as Linux or macOS. You will also need to have the Bash shell installed on your system. Most Unix-based systems come with Bash pre-installed, but if you are using a different shell or are unsure if Bash is installed on your system, you can check by running the echo $SHELL command. If the output is /bin/bash, then Bash is installed on your system.

Syntax and Examples

The syntax of the Bash for loop is as follows:

for (( expression1; expression2; expression3 ))
do
    command1
    command2
    ...
    commandN
done

The expression1 is used to initialize the loop counter. The expression2 is used to test the loop counter and decide whether to continue looping or not. The expression3 is used to increment the loop counter.

Here is an example of how you could use the Bash for loop to print the numbers from 1 to 10:

for (( i=1; i<=10; i++ ))
do
    echo $i
done

This would output the following:

1
2
3
4
5
6
7
8
9
10

You can also use variables in the expression1, expression2, and expression3 fields. For example, you could use the following loop to print the numbers from 1 to 10, using the variable start to specify the starting value and the variable end to specify the ending value:

start=1
end=10
for (( i=$start; i<=$end; i++ ))
do
    echo $i
done

You can also use the Bash for loop to iterate over a list of items. For example, you could use the following loop to print the names of all the files in the /etc directory:

for file in /etc/*
do
    echo $file
done

This would output a list of all the files in the /etc directory, such as:

/etc/hosts
/etc/passwd
/etc/services
...

Surprising Fact

Did you know that the Bash for loop can be used to iterate over a range of numbers in reverse order? To do this, you can use the seq command to generate a list of numbers in reverse order, and then use the Bash for loop to iterate over the list.

For example, the following loop will print the numbers from 10 to 1 in reverse order:

for i in `seq 10 -1 1`
do
    echo $i
done

This would output the following:

10
9
8
7
6
5
4
3
2
1

5 Examples or Tips to Increase Productivity

  1. Use the Bash for loop to automate repetitive tasks. For example, if you need to perform a series of tasks on multiple files, you can use a Bash for loop to iterate over the list of files and perform the tasks on each file in turn. This can save you a lot of time and effort compared to manually performing the tasks on each file.
  2. Use the Bash for loop to perform tasks on a schedule. For example, you could use a Bash for loop to run a script every hour, every day, or every week. This can be done using the cron utility, which allows you to schedule tasks to run at specific times or intervals.
  3. Use the Bash for loop to iterate over a list of items and perform actions on each item. For example, you could use a Bash for loop to iterate over a list of users and perform tasks such as adding users to a group, changing user permissions, or deleting users.
  4. Use the Bash for loop to automate system maintenance tasks. For example, you could use a Bash for loop to check the status of system services, restart services that have stopped, or alert you if a service is not running as expected.
  5. Use the Bash for loop to create custom scripts and utilities. For example, you could use a Bash for loop to create a script that checks the availability of a website or web server, or a script that performs backups of important files and directories.

Example script

Creating a bash script to search the current directory for the 5 most recently modified files.

#!/bin/bash

# Store the list of files
files=$(ls -t)

# Loop through the list of files
for file in $files; do
  # Check the last modified date of the file
  if [ $(date -r $file +%s) -gt $(date --date="5 days ago" +%s) ]; then
    # If the file was modified in the last 5 days, print it out
    echo $file
  fi
done

Challenge

Now that you have learned about the Bash for loop, try using it to create a script that performs the following tasks:

  1. Iterates over a list of numbers from 1 to 10
  2. For each number, checks if it is odd or even
  3. If the number is odd, prints “odd”
  4. If the number is even, prints “even”

To test your script, you can use the following list of numbers: 1 2 3 4 5 6 7 8 9 10. The output of your script should be as follows:

odd
even
odd
even
odd
even
odd
even
odd
even

Leave a Reply

Your email address will not be published. Required fields are marked *

1 × one =