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.
We save a file using Ctrl+O.
Step 2 – Write a for loop for 3 items
Step 3 – Make the script executable
chmod +x fruit.sh
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”
User Input to “FEED DATA” into our FOR LOOP
echo -en “Please tell me your favourite fruit: “
for fruit in $fruit
University of South Wales Reading list
PARKER, S. 2011. Shell Scripting – Expert recipes for Linux, Bash, and More. Indianapolis: John Wiley & Sons.