bash scripting

Bash scripting Part2 – For and While Loops With Examples

In the previous post, we talked about how to write a bash script, and we saw how bash scripting is awesome. In this post, we will look at the structured commands that control the flow of your shell scripts. This post demonstrates for loop and while loop in bash scripts.

for Command

The bash shell provides the for command to enable you to form a loop on a list of values. This is often the fundamental format of the for command.

for myvar in vars


Code Here


In every loop, the variable myvar holds the current value in the items list. The loop iterates until the end of the items list.

Iterating over simple values

The most basic use of the for command in bash scripting is to iterate over a list of simple values like this:

Check the results:

bash scripting for loop

Iterating over complex values

Your list maybe contains a comma or two words, and you want to deal with them as one item on the list.

Check the following example:

We quote our strings with double quotations.

We play nice till now, we always do. Just keep reading and practicing.

bash scripting compelx for loop

Reading values from a command

By using command substitution using this format $(Linux command) to execute any command that produces output.

This example uses the cat command to display the contents of the file. Notice that our file contains one word per line, not separated by spaces.

bash scripting loop from command

The for command iterates over the output of the cat command, assuming that each line has one word. What about having a space in one of these lines?

If you list that contains words with spaces between them, the for command treats each word as a separate value. This is because of the field separator.

The field separator

This problem because of a special environment variable called the internal field separator or IFS. By default, the following characters treated as fields.

  • Space
  • Tab
  • newline

If your text includes any of these characters, the bash will assume it’s a new field.

Well, you can change the IFS environment variable. Assume that you need to separate by new lines, so it will be like this:


So after you add this to your bash script, it will ignore spaces and tabs and will consider new lines as a separator.

You got it. Bash scripting is easy.

bash scripting passwd file

The separator is colons in /etc/passwd file which contains the user’s information, you can assign it like this:


Bash scripting is awesome, right?

Iterating over directory files

One of the most common things when using for loop in bash scripting is iterating over files in a directory and deal with them.

For example, we want to list the file in /home directory, so the code will be like this:

From the previous post, you should know the if statement and how to check for files and folders, so if you don’t know, I recommend you to review it bash script step by step.

bash scripting directory iteration

Here we use wildcard character which is the asterisk * and this is called in bash scripting file globbing which is a process of producing filenames automatically that match the wildcard character, in our case the asterisk means All files with all names.

Notice that in the if statements here we quote our variables with quotations because maybe the file or the folder name contains spaces.

As you see the result, all files and directories in that folder are listed.

for Command C-Style

If you know C language, you may find that the for loop here is weird because you are familiar with this syntax:

for (i = 0; i < 10; i++)


printf(“number is %d\n”, i);


The bash scripting has the for loop that looks like the C-style for loop with a little difference, here’s the syntax.

for (( variable assignment ; condition ; iteration process ))

So it looks like this:

for (( var = 1; var < 5; var++ ))

You can use the for C-style like this:

And this is the output:

bash scripting c-style

The while Command

The for loop is not the only way for looping in bash scripting. With while command, you can iterate through a set of commands.

First, it checks the test command at the beginning of each iteration. When the test command fails, the while command stops.

The while loop command takes the following structure:

while test command


other commands


and here is an example:

The script is simple; it starts with the while command to check if number is greater than zero, then the loop will run and the number value will be decreased every time by 1 and on every loop iteration it will print the value of number, Once the number value is zero the loop will exit.

bash scripting while loop

If we don’t decrease the value of var1, it will be the same value and the loop will be infinite.

Nesting Loops

You can type loops inside loops. This is called the nested loop.

Here’s an example of nested loops:

As you can see from the results, the outer loop hits first, then goes into the inner loop and completes it and go back to the outer loop and so on.

bash scripting nested loops

Looping on File Data

This is the most common usage for the for loop in bash scripting.

We can iterate over file content, for example, iterate over /etc/passwd file and see the output:

Here we have two loops, the first loop iterate over the lines of the file and the separator is the newline, the second iteration is over the words on the line itself and the separator is the colon :

bash scirpting file data

You can apply this idea when you have a CSV or any comma separated values file. The idea is the same; you just have to change the separator to fit your needs.

Controlling the Loop

Maybe after the loop starts you want to stop at a specific value, will you wait until the loop is finished? Of course no, there are two commands help us in this:

  • break command
  • continue command

The break command

The break command is used to escape from a loop. The break command is used to exit any loop, like the while and the until loops.

The for loop iterates over all the values until it reaches the break command which exits the loop.

bash scirpting break command

And the same for the while loop:

The break command exits the while loop and that happens when the execution reaches the if statement.

bash scipting break while

The continue command

The continue command is used to stop processing the rest of the commands inside a loop without exiting the loop.

Check the following example:

When the if condition is true, the continue command runs, which continues running the loop and never touch the rest of the commands in the loop.

bash scripting continue command

Redirecting the Loop Output

If you put the done command at the end of the loop, it will redirect the output of the loop to the file you specify.

The shell creates the file myfile.txt and the output is redirected to the file, and if we check that file we will find our loop output inside it.

bash sciprintg process output

Let’s employ our bash scripting knowledge in something useful.

Useful Examples

Finding executables

If you want to find the executable files that are available on your system, you can scan all the folders in the PATH environment variable. We discussed for loop and if statements and file separator so our toolset is ready. Let’s combine them together and make something useful.

This is just awesome. We were able to get all the executables on the system that we can run.

bash scripting finding executables

Now nothing stops you except your imagination.

I hope you learn a new thing or at least review your knowledge if you forget it. My last word for you, keep reading and practicing.

Thank you.

  • Pingback: Why Linux bash scripting is awesome | Ace Infoway()

  • Pingback: cron.weekly issue #67: Kubernetes, Brotli, Beehive, Takeover, MySQL 8, Nomad, tmux, fail2ban & more()

  • Pingback: Linux bash scripting the awesome guide part3 - Like Geeks()

  • clfapujc

    In the last example there is:

    for folder in $PATH
    How does the script know that we are looking for actual folders. We never used -d to specify we wanted directories.

    • When we use the asterisk * it means all files and directories in that folder.
      so we get files and directories BOTH of them without filtering .
      the filtration to get the folders is on the second if statement which is.

      for file in $folder/*

      then we search for the executable with -x

      • clfapujc

        Now I get it. Thanks.
        For a nearly total noob it is confusing 😛

    • Amani Hamis

      If I understood your question: I think the $PATH global variable only contains a list of folders in which the executables are stored, separated by “:”. To see these folders on your system just type echo $PATH. On my system, it returns the string “/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/snap/bin”
      None of those can be a file, so he didn’t need to filter.

      • Great comment really.
        Thanks for contribution.

        • Amani Hamis

          GREAT articles btw! I’m learning SO MUCH!
          I started at Linux file system->Main Linux Commands->Main Linux Commands (Part 2) -> Linux Environment Variables -> Linux Command Line Tricks -> Bash Script Step-by-Step ->Bash Scripting Part 2 -> Part 3 where I am currently 🙂
          Thanks, and don’t stop delivering!

          • Great to know that.
            I do my best to post quality content.
            Hope the community love it.

  • Pingback: 31+ Examples for sed Linux command in text manipulation - Like Geeks()