Creating a For Loop Bash Script in Linux

For Loop Bash. For is a loop command in Linux; A bash command used in Linux and other Unix like operating systems to loop through a series of commands. It is most commonly used in shell scripts or bash to help automate tasks or routines.

How to Create a For Loop Bash Script in Linux

To make a For Loop Bash Script, in Linux or Unix, the basic syntax is as follows:

for variable in list

Here is how the for loop bash script above, works.

  1. The script starts with the shebang line #!/bin/bash, which tells the system to use the bash shell to interpret the script.
  2. The for Linux command starts by defining a variable to hold the current item in list.
  3. The list can be used to represent any list of items, such as filenames, directories, or strings. The for command will loop through each item in the list and assign it to the variable.
  4. For each item in the list, the commands defined between the do and done block will be executed.
  5. Once all items in the list have been processed, the loop is stopped with done, clearing the variable for use elsewhere, and the for command is complete

A Fruity Looking For Loop Bash Script

In the following example, the for command is used in a bash script to iterate over a list of three common fruits (apple, banana, and orange) and assigning each one to the fruit variable. For each fruit in the list, the echo command is used to print a message to the screen that includes the fruit name.

for fruit in apple banana orange 
  echo "I like to eat $fruit" 

When you run this script, you'll see output that looks like this. Notice how a new line is printed for each fruit.

I like to eat apple
I like to eat banana
I like to eat orange

Note: you can also use * wildcards to automatically populate a list of items to iterate through, as you'll see in this next example.

For Loop Bash Script to iterate through a list of filenames

for file in *.txt
  echo "Processing file: $file"
  # add your commands to process for each file here

In the example above, the for command is used to loop through all files within the current directory that have a .txt extension. The echo command is simply being used to print the name of each file to the screen. You can add your own commands to process each file as needed by replacing everything following the # commented line in the code.

For Loop Bash Script to iterate through a list of numbers

You can also use the for loop command in a Linux bash script to iterate through a list of numbers as shown below.

for i in {1..10}
  echo "Number: $i"
  # add your commands to process for each number here

How bash script for loop numerical iteration works:

In the previous example, the 'for' command is used to loop through numbers 1 through 10. The 'echo' command is also being used to print each number to the screen. Again, you can replace the commented line with your own commands to process with each line number as needed.

As you can see, the for command can be a powerful tool to use for automating tasks through shell scripts while running from Linux operating systems. With a little practice, you'll be using it to create cool scripts up front that save you a lot of time and effort in the long run.

If you found this post useful, you might also be interested in learning how to chain commands using And Linux Command.