Professional Documents
Culture Documents
Arrays Bash Tutorial2
Arrays Bash Tutorial2
com/array-loops-in-bash/
In this article we'll show you the various methods of looping through arrays in
Bash. Array loops are so common in programming that you'll almost always
need to use them in any signi�cant programming you do. To help with this,
you should learn and understand the various types of arrays and how you'd
loop over them, which is exactly what we present in this article.
Before we proceed with the main purpose of this tutorial/article, let's learn a
bit more about programing with Bash shell, and then we'll show some
common Bash programming constructs.
Most Unix-like operating systems come with a shell such as the Bash
Privacy
1 of 16 10/22/20, 4:04 PM
Array Loops in Bash https://stackabuse.com/array-loops-in-bash/
The Bash shell is an improved version of the old Bourne shell, which was one
of the �rst Unix/Linux shell in general use by the user base. It had limited
features compared with today's shells, and due to this most of the
programming work was done almost entirely by external utilities.
To �nd the type or version of Bash you're OS has installed just type the
following command:
$ echo $BASH_VERSION
3.2.57(1)-release
Privacy
2 of 16 10/22/20, 4:04 PM
Array Loops in Bash https://stackabuse.com/array-loops-in-bash/
cat : Display content in a �le or combine two �les together. The original
There are many more basic commands not mentioned here, which you can
easily �nd information on given the extensive amount of documentation on
the internet. Although, learning the basic commands above will teach you
much of what you need to know.
You might notice throughout this article that every �rst line of a shell script
begins with a shebang (https://en.wikipedia.org/wiki/Shebang_(Unix)) or
hash-bang. It is a special type of comment which tells the shell which
program to use to use to execute the �le. For shell scripts, this is the
#!/bin/bash line.
Now that you've been exposed to some common Bash commands, it's time to
understand how to use arrays and loops. And �nally we'll show some real-
world examples of how you can loop over arrays in Bash scripts.
Privacy
3 of 16 10/22/20, 4:04 PM
Array Loops in Bash https://stackabuse.com/array-loops-in-bash/
Loops in Bash
"Loops", or "looping", is simply a construct in which you execute a particular
event or sequence of commands until a speci�c condition is met, which is
usually set by the programmer. We have three types of loops available to us
in Bash programming:
while
for
until
While Loop
If you have ever programmed before in any language, you probably already
know about looping and how you can use it to control the �ow of a program
or script in addition to the if , elif , and else . Looping allows you to
iterate over a list or a group of values until a speci�c condition is met.
while <list>
do
<commands>
done
4 of 16 10/22/20, 4:04 PM
Array Loops in Bash https://stackabuse.com/array-loops-in-bash/
#!/bin/bash
count=0
while [ $count -le 10 ]
do
echo "$count"
count=$(( $count + 1 ))
done
The condition here is $count -le 10 , which will return a true value as
long as the $count variable is less than or equal ( -le ) to 10. Every time this
condition returns true , the commands between do and done are executed.
Subscribe
Until Loop
In addition to while , we can also use the until loop which is very similar
to the while loop. The syntax of the until loop is the same as the while
Privacy
5 of 16 10/22/20, 4:04 PM
Array Loops in Bash https://stackabuse.com/array-loops-in-bash/
loop, however the main difference is that the condition is opposite to that of
while. Here the loop commands are executed every time the condition fails,
or returns false .
#!/bin/bash
count=0
until [ $count -gt 10 ]
do
echo "$count"
count=$(( $count + 1 ))
done
Here the loop is executed every time $count is not greater than ( -gt ) 10.
For loop
Syntactically the for loop is a bit different than the while or until loops.
These types of loops handle incrementing the counter for us, whereas before
we had to increment it on our own.
#!/bin/bash
Within the loop condition we tell it which number to start the counter at
( n=1 ), which number to end the counter at ( n<=10 ), and how much to
increment the counter by ( n++ ).
Privacy
6 of 16 10/22/20, 4:04 PM
Array Loops in Bash https://stackabuse.com/array-loops-in-bash/
#!/bin/bash
Here we execute the loop for every string instance, which in this case is
"Kate", "Jake", and "Patt".
Arrays in Bash
In Bash, there are two types of arrays. There are the associative arrays and
integer-indexed arrays. Elements in arrays are frequently referred to by their
index number, which is the position in which they reside in the array. These
index numbers are always integer numbers which start at 0.
Associative array are a bit newer, having arrived with the version of Bash 4.0.
Privacy
7 of 16 10/22/20, 4:04 PM
Array Loops in Bash https://stackabuse.com/array-loops-in-bash/
#!/bin/bash
array=( A B C D E F G )
echo "${array[0]}"
echo "${array[1]}"
echo "${array[2]}"
echo "${array[3]}"
echo "${array[4]}"
echo "${array[5]}"
echo "${array[6]}"
In this article we're going to focus on integer-indexed array for our array
loops tutorial, so we'll skip covering associative arrays in Bash for now, but
just know that it is god to be aware of their existence.
In our simple example below we'll assume that you want display a list of your
website's registered users to the screen. The list of users is stored in the
variable users , which you need to loop over to display them.
#!/bin/bash
8 of 16 10/22/20, 4:04 PM
Array Loops in Bash https://stackabuse.com/array-loops-in-bash/
With this syntax, you will loop over the users array, with each name being
temporarily stored in u . The [@] syntax tells the interpreter that this is an
indexed array that we'll be iterating over.
There are quite a few ways we can use array loops in programming, so be
sure not to limit yourself to what you see here. We've simply shown the most
basic examples, which you can improve upon and alter to handle your use-
case.
(/author/scott/)
About Scott Robinson (/author/scott/)
Twitter (https://twitter.com/ScottWRobinson)
9 of 16 10/22/20, 4:04 PM
Array Loops in Bash https://stackabuse.com/array-loops-in-bash/
Subscribe
Ad
Follow Us
Privacy
10 of 16 10/22/20, 4:04 PM
Array Loops in Bash https://stackabuse.com/array-loops-in-bash/
(https://twitter.com (https://stackabuse.com
/StackAbuse) (https://www.facebook.com /rss/)
/stackabuse)
(https://gum.co/data-visualization-in-python)
(https://gum.co/data-visualization-in-python)
Understand your data better with visualizations! With over 275+ pages, you'll learn the ins
and outs of visualizing data in Python with popular libraries like Matplotlib, Seaborn, Bokeh,
and more.
Privacy
11 of 16 10/22/20, 4:04 PM
Array Loops in Bash https://stackabuse.com/array-loops-in-bash/
Newsletter
Subscribe
Ad
12 of 16 10/22/20, 4:04 PM
Array Loops in Bash https://stackabuse.com/array-loops-in-bash/
Backend/Fullstack Engineer
Bravado 23 days ago (https://hireremote.io/remote-job/3007-backend-fullstack-engineer-at-
bravado)
python (https://hireremote.io/remote-python-jobs)
Privacy
13 of 16 10/22/20, 4:04 PM
Array Loops in Bash https://stackabuse.com/array-loops-in-bash/
(https://stackabu.se/daily-coding-problem)
Ad
Privacy
14 of 16 10/22/20, 4:04 PM
Array Loops in Bash https://stackabuse.com/array-loops-in-bash/
Recent Posts
Privacy
15 of 16 10/22/20, 4:04 PM
Array Loops in Bash https://stackabuse.com/array-loops-in-bash/
Tags
Follow Us
(https://twitter.com (https://stackabuse.com
/StackAbuse) (https://www.facebook.com /rss/)
/stackabuse)
Privacy
16 of 16 10/22/20, 4:04 PM