Bash While Loop: An Easy Way to Print Odd Numbers from 1 to N

Claudio Sabato
3 min readApr 2, 2020

In this article you will learn how to create a while loop in Bash.

While loops are used in Bash scripting and in many other programming languages…

The loop will go through N numbers and print only the odd ones.

You will see how our script looks like if we hardcode the value of N in it, and then you will learn how to pass the value of N to the script as an argument via the Linux command line.

This applies to any Bash shell running on Unix-like systems (including MacOS).

First of all let’s have a look at the generic structure of a while loop in Bash:

while [ condition ]
do
command1
command2
...
commandN
done

Here is how a while loop works…

As long as the condition between square brackets is true the commands between do and done are executed.

How does this apply to this specific case?

INDEX=0 while [ INDEX < N ]
do
if [ $INDEX is Odd ]
then
print $INDEX
fi
increase $INDEX
done

And if we translate this logic into an actual Bash script…

#!/bin/bashINDEX=0while [ $INDEX -lt 20 ]
do
REMAINDER=$(( $INDEX % 2 ))
if [ $REMAINDER -ne 0 ]
then
echo $INDEX
fi
INDEX=$(($INDEX+1))
done

Let’s review what this script does:

  1. Set the value of the variable INDEX to 0.
  2. Start a while loop that is executed if the value of INDEX is less than 20.
  3. At every iteration of the loop calculate the REMAINDER for the division of the INDEX by 2.
  4. Check if the REMAINDER is not zero…in that case print INDEX because it’s an odd number
  5. Increment the value of INDEX by 1 before executing the next iteration of the while loop

Note: Arithmetic operations can be executed in a Bash script using $(( )).

Here is what we see if we execute it:

myuser@localhost:~$ ./print_odd_numbers.sh
1
3
5
7
9
11
13
15
17
19

It works well!

Passing N as an Argument Through the Command Line

We now want to pass N via the command line.

To do that we will have to apply the following changes:

  • Use a variable called N and assign to it the first argument passed to the script
  • Replace 20 with N in the condition of the while loop
#!/bin/bashN=$1
INDEX=0
while [ $INDEX -lt $N ]
do
REMAINDER=$(( $INDEX % 2 ))
if [ $REMAINDER -ne 0 ]
then
echo $INDEX
fi
INDEX=$(($INDEX+1))
done

What is $1?!?

It’s the variable that in Bash contains the first argument passed to a script.

Execute the script again, this time passing N via the command line:

myuser@localhost:~$ ./print_odd_numbers.sh 20
1
3
5
7
9
11
13
15
17
19

The script works well, so our changes are correct 🙂

Conclusion

In this tutorial you have learned:

  • The structure of a while loop in Bash.
  • How to use an if statement nested in a while loop.
  • The way you can use the arithmetic operator to calculate the remainder of a division.
  • How an index variable can be used in a while loop.
  • The technique to pass arguments to a Bash script via the command line.

Also, sometimes you might have the need to delay some of the commands executed in your while loops. To do that you would use the Bash sleep command.

And now, what else will you create with this knowledge? 😀

Originally published at https://codefather.tech on April 2, 2020.

--

--

Claudio Sabato

Claudio Sabato is an IT expert with over 15 years of professional experience in Python/Bash programming, Linux Systems Administration and IT Systems Design.