Bash Script Basic For Loop Example

In this article, we will explore the for loop in Bash scripting through a practical example. The for loop is a fundamental control flow tool that allows you to iterate over a list or a range of values. Understanding how to effectively use for loops can significantly enhance your scripting capabilities.

Prerequisites

  • Basic knowledge of Bash syntax
  • Understanding of variables and conditions
  • Familiarity with command line execution

DID YOU KNOW?

In scripting, loops can drastically reduce code repetition, making scripts more efficient and easier to maintain!

The Script

Below is a simple example of a for loop in a Bash script. This script iterates through a list of fruits and echoes each one to the terminal.

#!/bin/bash

for fruit in apple banana cherry
do
    echo "I like $fruit"
done

Step-by-Step Explanation

NOTE!

This example uses a basic for loop to demonstrate iteration. Using loops effectively can help automate repetitive tasks in your shell scripts.

Let’s break down the script step by step:

  1. Shebang: The first line #!/bin/bash indicates that the script should be run in the Bash shell.
  2. For Loop Structure: The statement for fruit in apple banana cherry initializes the loop, defining the variable fruit that will take on each value in the list.
  3. Loop Body: The lines within do and done represent the actions performed for each iteration. Here, we are using echo to print the message.

How to Run the Script

To execute this script, follow these steps:

  1. Create a new file, for example, fruits.sh, and paste the script into it.
  2. Make the script executable with the command: chmod +x fruits.sh.
  3. Run the script using: ./fruits.sh.
Bash Script Basic For Loop Example
Bash Script Basic For Loop Example

Conclusion

The for loop is an essential component of Bash scripting that allows for effective iteration over lists or ranges. By mastering this concept, you can create more dynamic and efficient scripts that save you time and effort in your daily programming tasks.

FAQ

  1. What is a for loop in Bash?

    A for loop is a control flow statement that allows you to execute a block of code multiple times, iterating through a set of values.

  2. Can I loop through numbers?

    Yes, you can loop through numbers using the syntax for ((i=1; i<=5; i++)).

  3. What happens if I miss a ‘do’ keyword?

    If you miss the ‘do’ keyword, the script will not execute properly and will return a syntax error.

  4. Can I use functions inside a for loop?

    Absolutely! You can call functions within the loop to perform more complex operations.

  5. Is it possible to loop through files in a directory?

    Yes, you can loop through files using a glob pattern, such as for file in *.txt.

Troubleshooting

If you encounter issues with your Bash scripts, here are some common error messages and their fixes:

  • Syntax error near unexpected token `done’: This usually indicates that you have improperly formatted your loop with missing keywords like do.
  • Permission denied: If you see this when trying to run your script, ensure that you have made the script executable with chmod +x script_name.sh.
  • Command not found: This error means that you are trying to call a command that is not defined or installed. Ensure all commands are correctly typed and installed.