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 incredible.

In this tutorial, we will look at the for and while commands and how to make loops to iterate over a series of values.


The for loop




for myvar in vars; do

Code Here


In every loop, the variable myvar holds one of the values of the list. The loop iterates until the list is finished.


Iterating over simple values

You can iterate over simple values like this:


for var in first second third fourth fifth; do

	echo The $var item


Check the results:

Linux Bash Scripting For and While Loops


Iterating over complex values

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

Check the following example:


for var in first "the second" "the third" "I’ll do it"; do

	echo "This is: $var"


We quote our strings with double quotations.

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

compelx for loop


Command substitution

By using command substitution using this format

$(Linux command)

you can store the result in a variable for later use.



for var in $(cat $my_file); do

	echo " $var"


Here we get the file content using cat command. Notice that our file contains one word per line, not separated by spaces.

loop from command

Here we get the content of the file using command substitution then iterate over the result, assuming that each line has one word.

What about having spaces in one of these lines?

In this case, every word will be considered a field. You need to tell the shell to consider new lines as a separator instead of spaces.


The field separator

By default, the following characters treated as fields.

  • Space
  • Tab
  • newline

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

Well, you can change the internal field separator or IFS environment variable. Like this:


It will consider new lines as a separator instead of spaces.




for var in $(cat $file); do

	echo " $var"


You got it. Bash scripting is easy.

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 fantastic, right?


Iterating over directory files using the for loop

If you want to list the files in /home directory, you can use the for loop like this:


for obj in /home/likegeeks/*; do

	if [ -d "$obj" ]; then

		echo "$obj is a folder"

	elif [ -f "$obj" ]; then

		echo "$obj is a file"



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 review it Bash script step by step.

directory iteration

Here we use a wildcard character, which is the asterisk * and this is called in Bash scripting file globbing, which 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 loop C-Style

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

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


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


Well, you can use the same syntax, but with a little difference, here’s the syntax.

for (( variable = start ; condition ; iteration step))

So it looks like this:

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

And this is an example:


for ((var = 1; var <= 10; var++)); do

	echo "number is $var"


And this is the output:


The while loop

The for loop is not the only way for looping in Bash scripting. The while loop does the same job, but it checks for a condition before every iteration.



loop command takes the following structure:

while condition; do



and here is an example:



while [ $number -gt 4 ]; do

	echo $number

	number=$(($number - 1))


The script is simple; it starts with the while command to check if the 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 the number, Once the number value is zero the loop will exit.

while loop

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


Nested Loops

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

Here’s an example of nested loops:


for ((v1 = 1; v1 <= 5; v1++)); do

	echo "Start $v1:"

	for ((v2 = 1; v2 <= 5; v2++)); do

		echo " Inner loop: $v2"



The outer loop hits first, then goes into the internal loop and completes it and goes back to the outer loop and so on.

nested loops

Iterate over file lines

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

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



for text in $(cat /etc/passwd); do

	echo "This line $text ++ contains"


	for field in $text; do

		echo " $field"



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

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, two commands help us in this:

  • The break command
  • The continue command

The break Command

You can use the break command to exit from any loop, like the while and the until loops.


for number in 10 11 12 13 14 15; do

	if [ $number -eq 14 ]; then



	echo "Number: $number"


The loop runs until it reaches 14 then the command exits the loop.


break command

And the same for the while loop:



while [ $val -lt 5 ]; do

	if [ $val -eq 4 ]; then # Check number value

		break # The Code Breaks here <==


	echo "Iteration: $val" # The Printed Message

	val=$(($val + 1))


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

break while

The continue command

You can use the command to stop executing the remaining commands inside a loop without exiting the loop.

Check the following example:


# The loop starts here

for ((number = 1; number < 10; number++)); do

	if [ $number -gt 0 ] && [ $number -lt 5 ]; then # Check if number greater than 0 and less than 5



	echo "Iteration number: $number" # The printed message


When the if condition is true, the command never run until the condition becomes false.

continue command

Redirecting the loop output

You can use the command to send the loop output to a file like this:


for ((var = 1; var < 10; var++)); do

	echo "Number is $var"

done >myfile.txt

echo "finished."

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.

process output

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


Useful examples

Finding executables

To get all executable files on your system, you can iterate over the directories in the PATH variable. We discussed for loop, and if statements and file separator so, our toolset is ready. Let’s combine them and make something useful.



for dir in $PATH; do

	echo "$dir:"

	for myfile in $dir/*; do

		if [ -x $myfile ]; then

			echo " $myfile"




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

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.

8 thoughts on “Bash Scripting Part2 – For and While Loops With Examples
  1. In the last example there is:

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

    1. 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

    2. 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.

        1. 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!

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

Leave a Reply

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