Bash for loop N times

Posted in Bash, REGEX on October 25, 2016 · 2 mins read
How can I loop through something a number of times in Bash?

I at times dread using for loops as it can turn into a run-on infinite script that I have to terminate in the terminal with control+Z. Today's post I will discuss what I've used over time when I want to loop through something a certain number of times.

We start by creating a directory and pointing to that directory in one line with:

cd; cd Desktop; mkdir foobar; cd foobar

The above one line command explained:

cd # points to main directory
cd Desktop # point terminal to the Desktop
mkdir foobar # creates a folder named foobar
cd foobar # points terminal into the new directory

Next we will open BBEdit or a similar text editor such as Sublime, Textmate or Notepad++ and create a bash file named loop_fun.sh and save that in our new directory foobar.

In the text editor we must specify that this is a bash file with a shebang:

# !/bin/bash

on the first line of our loop_fun.sh file. What is a shebang you might ask? A shebang tells the shell what program to interpret the script with when executed.

After adding the shebang we can create our global variable that will be used to tell our for loop how many times it should run:

NUMBER=10

if you want to see the global variable value in the terminal we can echo it in the file:

NUMBER=10
echo $NUMBER

when you execute the bash script in the terminal with bash loop_fun.sh your terminal should render the echo count of 10.

Let's comment out our echo command by placing an # before echo. After commenting out the echo we can create our for loop:

for (( count=1; count <= "$NUMBER"; count++ )); do
    echo $count
done

The break down of the for loop:

count=1 # setting the numerical value of the variable count
count <= "$NUMBER" # count variable value 1 continues until count is equal to $NUMBER
count ++ # every time the loop runs it adds 1 to the variable count

There are several variations when creating the loop such as not needing the semi-colon before do if do is on a new line:

for (( count=1; count <= "$NUMBER"; count++ ))
do
    echo $count
done

I am one that likes to limit the need for more lines then needed so I will always code my for loops with ; do but there are other ways to declare your global variable in the for statement such as:

for (( count=1; count <= $NUMBER; count++ )); do

or

for (( count=1; count <= NUMBER; count++ )); do

but traditionally I like to always declare my variables with "$variable" as this is the appropriate method when it comes to conditionals and regex but those are different topics for a later time.

With the new loop created and saved to the file you can now execute the bash script in your file with bash loop_fun.sh. If you wanted to execute the script without needing the bash before the filename you can use chmod:

chmod +x loop_fun.sh

Then just use loop_fun.sh but there are some pre-cautions when it comes to using chmod so be careful.

The above sample is great but if you are lazy like me and don't want to open the file every time to set a number in the global variable $NUMBER you can add a little osascript with a conditional to or bash file:

NUMBER="$(osascript -e 'Tell application "System Events" to display dialog "How many times would you like to loop?:" default answer ""' -e 'text returned of result' 2>/dev/null)"
if ! [[ "$NUMBER" =~ ^[0-9]+$ ]]; then
    echo
    echo "You did not enter in an integer"
    echo
    exit 1
else
    for (( count=1; count <= "$NUMBER"; count++ )); do
        echo $count
    done
fi
Help the site go ad free:
Storm Trooper