Skip to content

KALI – BASH SCRIPTING – How to write a FOR loop – The Visual Guide


The for loop starts with a list of items and works it’s way through them until it reaches the end.  A for loop does not test conditions.

Step 1 – Root Terminal and Nano editor

Start a root terminal, then enter the word nano – the nano editor will appear.

nano editornano open

We save a file using Ctrl+O.

 Step 2 – Write a for loop for 3 items


kali FOR

Step 3 – Make the script executable

chmod +x


kali fruit for loop running

Step 4 – Analysis of For Loop

The for loop is reading in a fixed list (apples, orange, pear).

It assigns a variable called fruit – and writes each item of the list into this variable

The first time fruit=apple, second time fruit=orange, third time fruit=pear.  The variable fruit is changing as it runs through our fixed list.

Do is the start of the loop.

Done shows the end of the loop.

After the loop is complete, it prints out “lets make a fruit salad”!.


Different ways of “feeding” the FOR LOOP with data

 fruit = “apple orange pear”

kali fruit variable 2


kali fruit salad for tea****

User Input to “FEED DATA” into our FOR LOOP


fruituserCode for User Input

echo -en “Please tell me your favourite fruit: “

read fruit

for fruit in $fruit

user input 2Make executeable

fruituser chmod rights User Input read in, and used in code

fruit user outputEnter multiple fruits – output on a new line for each item in list


multiple fruit


University of South Wales Reading list

PARKER, S.  2011.  Shell Scripting – Expert recipes for Linux, Bash, and More.  Indianapolis: John Wiley & Sons.


KALI – First things to do after installing Kali Debian Linux – The Visual Guide

Leave a Comment

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s

%d bloggers like this: