2018-11-14 13:17:52 +00:00
|
|
|
---
|
|
|
|
layout: page
|
|
|
|
category-page: scripts
|
|
|
|
category-title: Scripting
|
|
|
|
tags: array variable many index selector
|
|
|
|
author: Marco Tereh
|
|
|
|
title: Arrays
|
2018-11-15 08:29:14 +00:00
|
|
|
previous-page: pages/scripts/parameter-expansion.html
|
|
|
|
next-page: while-loop.html
|
2018-11-14 13:17:52 +00:00
|
|
|
---
|
|
|
|
|
2018-11-15 08:29:14 +00:00
|
|
|
There is a special kind of <a href="variables.html">variable</a>, called an array.<br>
|
2018-11-14 13:17:52 +00:00
|
|
|
|
|
|
|
While variables hold a single value, arrays hold many. They could be called a list of variables.
|
|
|
|
The simplest way to create an array is using this format:
|
|
|
|
|
|
|
|
{% highlight bash %}
|
|
|
|
names=("Anna" "Bob" "Clarice" "Dee")
|
|
|
|
{% endhighlight %}
|
|
|
|
|
|
|
|
This will create an array, called <code>names</code>, which holds 4 values.
|
|
|
|
The values in the array can be accessed using the syntax
|
|
|
|
|
|
|
|
{% highlight bash %}
|
|
|
|
${names[SELECTOR]}
|
|
|
|
{% endhighlight %}
|
|
|
|
|
2018-11-15 20:09:21 +00:00
|
|
|
where SELECTOR is the selector associated to the desired element. There are two kinds of
|
|
|
|
arrays, depending on what kind of selector is used.<br>
|
2018-11-14 13:17:52 +00:00
|
|
|
|
2018-11-15 20:09:21 +00:00
|
|
|
<code>names</code> is an <i>indexed</i> array, meaning the selector is a number.
|
|
|
|
Since we didn't specify any when we defined it, <code>names</code>' selectors start at
|
|
|
|
0 and count up from there.<br>
|
2018-11-14 13:17:52 +00:00
|
|
|
|
|
|
|
Thus we can access the string <code>"Anna"</code> using <code>names[0]</code>.
|
|
|
|
In the same way we use <code>names[1]</code> to access <code>"Bob"</code> and so on.<br>
|
|
|
|
|
2018-11-15 20:09:21 +00:00
|
|
|
If we want to specify a particular index for our values when creating the array
|
|
|
|
we can do it like this:
|
2018-11-14 13:17:52 +00:00
|
|
|
|
|
|
|
{% highlight bash %}
|
|
|
|
names=([1]="Anna" [2]="Bob" [5]="Ernie" [12]="Luigi")
|
|
|
|
{% endhighlight %}
|
|
|
|
|
2018-11-15 20:09:21 +00:00
|
|
|
As you might have guessed, this will create an array with <code>"Anna"</code> at index 1,
|
|
|
|
<code>"Bob"</code> at index 2, <code>"Ernie"</code> at index 3 and so on.<br>
|
2018-11-14 13:17:52 +00:00
|
|
|
|
|
|
|
If our indices are all sequential and we just want to change the starting index, we can use
|
|
|
|
|
|
|
|
{% highlight bash %}
|
|
|
|
names=([12]="Luigi" "Mario" "Nate")
|
|
|
|
{% endhighlight %}
|
|
|
|
|
2018-11-18 17:39:51 +00:00
|
|
|
Which will create an array with <i>"Luigi"</i> at index 12,
|
|
|
|
<i>"Mario"</i> at index 13 and <i>"Nate"</i> at index 14.<br>
|
2018-11-14 13:17:52 +00:00
|
|
|
An indexed array's selector can also be negative, which will start counting from the end,
|
2018-11-18 17:39:51 +00:00
|
|
|
so <i>${names[-1]}</i> means <i>"Nate"</i>, <code>${names[-3]}</code> is
|
|
|
|
<i>"Luigi"</i> and so on.<br>
|
2018-11-14 13:17:52 +00:00
|
|
|
|
|
|
|
The other kind of array is an <i>associative</i> array.
|
|
|
|
In an associative array, the values are not mapped to a number but to some other string.<br>
|
|
|
|
|
|
|
|
Here's an example of an associative array:
|
|
|
|
|
|
|
|
{% highlight bash %}
|
|
|
|
colors=([white]="#FFFFFF" [black]="#000000" [red]="#FF0000" [yellow]="#00FFFF")
|
|
|
|
{% endhighlight %}
|
|
|
|
|
2018-11-15 20:09:21 +00:00
|
|
|
In this case it is not possible to omit the selector as there is no logical word
|
|
|
|
that "follows" white. To access its members, we use the same syntax but write a string
|
|
|
|
instead of a number as the selector.<br>
|
2018-11-14 13:17:52 +00:00
|
|
|
Arrays, both kinds, can also be modified after creation. We can use
|
|
|
|
|
|
|
|
{% highlight bash %}
|
|
|
|
colors[blue]="#0000FF"
|
|
|
|
{% endhighlight %}
|
|
|
|
|
|
|
|
to add an element to the array (or modify it if it exists already) and
|
|
|
|
|
|
|
|
{% highlight bash %}
|
|
|
|
unset colors[black]
|
|
|
|
{% endhighlight %}
|
|
|
|
|
|
|
|
to remove one.<br>
|
|
|
|
|
2018-11-15 20:09:21 +00:00
|
|
|
The <code>@</code> and <code>*</code> selectors are special - they cannot be written to but
|
|
|
|
reading from them will return <b>all</b> the values in the array, separated by spaces.
|
|
|
|
Similarly, <code>${!colors[@]}</code> is a list of
|
2018-11-14 13:17:52 +00:00
|
|
|
all the selectors (or indices) that were assigned a value.<br>
|
|
|
|
|
|
|
|
<a href="parameter_expansion.html">Parameter Expansion</a> works on indexed arrays as well,
|
|
|
|
much like it works on strings, but it manipulates the members of the array instead of characters.
|
|
|
|
For example:
|
|
|
|
|
|
|
|
{% highlight bash %}
|
|
|
|
${names[@]: 13}
|
|
|
|
{% endhighlight %}
|
|
|
|
|
|
|
|
is equivalent to <code>"Mario" "Luigi"</code>, while
|
|
|
|
|
|
|
|
{% highlight bash %}
|
|
|
|
${#names[@]}
|
|
|
|
{% endhighlight %}
|
|
|
|
|
2018-11-18 17:39:51 +00:00
|
|
|
counts the number of elements in the array, in this case 3.<br><br>
|
2018-11-14 13:17:52 +00:00
|
|
|
|
2018-11-15 20:09:21 +00:00
|
|
|
Further reading: <a href="http://www.gnu.org/software/bash/manual/bashref.html#Arrays">
|
|
|
|
the bash reference manual</a>
|