8- BASH Scripting – FOR Conditional Statement

Hello, Linux-fanatics, in our earlier tutorial we discussed advanced used of if-then-else conditional statements. In this tutorial, we will discuss for-conditional statement.

for statement is used where we require a set of commands to repeated over & over again until our conditions are all met. Like for example, we need to change some information for all users on system,

Syntax for for conditional statement 

for var (variable name) in list (parameters)
command (or commands)

Example Script

# testing for statement
for name in dan susan daniel
echo “Welcome $name”
echo “Ohhh, I forgot to welcome $name”

In this script, when for loop finishes last value of variable i.e. daniel, will be retained but we can reassign a new value i.e. monti.

Also, you should remember when using words like won’t or don’t , you should either escape those characters or use double quotation otherwise they will not be interpreted properly. For example:  won\’t don\’t   or “won’t” “don’t”

Now let’s take a look at another example

# script to show use of double quotes & escape character
for var in I don\’t want to go to “North korea”
echo “ word : $var”

So in above script we escaped don’t & used double quotations with North Korea, now the output for the script will show proper result.Try yourself to write above script with & without escaping  the characters & using double quotation. You will notice the difference yourself.

Also, if we want that for statement reads from a variable  or use output of a command as list, we can also do that

Example Script

# Reading list from a variable
name=”dan susan daniel”
for member in $name
echo “Welcome again $member”

Here we defined a varaible “name” which we then used in for statement, also further in next line we added another namme to variable.

Now, let’s create a file named “name.txt” in “/home/dan” & lets write up some name there.  We will now use the file as our input for list in for statement

# using a file as input
for member in $(cat $file)
echo “Welcome $member ”


IMPORTANT NOTE: When you created your file, you might have listed names either by typing one name per line, or using single space between names or using tab as space between names.

Note that if you have commas to separate than all the names would  be interpreted as single name, why it is so. That’s because of environmental variable known as IFS or internal field separator and IFS interprets space, tab, newline as separators.

There might be a need  when we don’t want space or tabs to used as separators or we might want to add “,” comma as a separator. It can be done by using following in your script


So, now only new line, comma & semi colon will be separators for the rest of the script.

Using for command in C language style

We can also for statement as is used in C programming language i.e.

for (( i=0; i=20; i++ ))

this will provide output of “I” starting from 1 until its equal to 20 with  increment of one.

With this approach you can also use multiple variable in single for statement

Example script

#using multiple variables with C style for statement
for (( a=0, b=20; a =20 ; a++ b– ))
echo “value of a is $a & value of b is $b”

Both ways of using for statement has merits and we can use them both depending on our need

Now that we have learned for conditional statement, let’s write a script with all conditional statement we have learned till now i.e. for-if-then-else

# script using for-if-then-else (directory & file checking)
for file in /home/dan/*
if [ -d “$file”]
echo”$file is a directory”
elif [ -f “$file”]
echo “$file is a file not a directory”

So, with that script this tutorial is now complete. We are now entering into deep territories of scripting . Remember don’t try to over-learn in a single day, be patient & practice as much as you can.

And leave your questions/queries down below. ADIOS for now !!!

If you think we have helped you or just want to support us, please consider these :-

Connect to us: Facebook | Twitter | Google Plus

Become a Supporter – Make a contribution via PayPal

[paypal_donation_button align=”left” border=”1″]

Linux TechLab is thankful for your continued support.


Passionate about Linux & open source. Loves to learn, read & write about Linux as well as new technologies.

You may also like...

Leave a Reply