1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
|
Bash arrays
===========
---
date: 2013-10-13
---
Way too many people don't understand Bash arrays. Many of them argue
that if you need arrays, you shouldn't be using Bash. If we reject
the notion that one should never use Bash for scripting, then thinking
you don't need Bash arrays is what I like to call "wrong".
The simple explanation of why everybody who programs in Bash needs to
understand arrays is this: command line arguments are exposed as an
array. Does your script take any arguments on the command line?
Great, you need to work with an array!
Normal array syntax
-------------------
<table>
<caption>
<h1>Setting an array</h1>
<p><var>tokens...</var> is expanded and split into array elements
the same way command line arguments are.</p>
</caption>
<tbody>
<tr>
<td><code>array=(<var>tokens...</var>)</code></td>
<td>Set the contents of the entire array.</td>
</tr><tr>
<td><code>array+=(<var>tokens...</var>)</code></td>
<td>Appends <var>tokens...</var> to the end of the array.</td>
</tr><tr>
<td><code>array[<var>n</var>]=<var>value</var></code></td>
<td>Sets an individual entry in the array, the first entry is at
<var>n</var>=0.</td>
</tr>
</tbody>
</table>
Now, for accessing the array. The most important things to
understanding arrays is to quote them, and understanding the
difference between `@` and `*`.
<table>
<caption>
<h1>Getting an entire array</h1>
<p>There is almost <em>no</em> valid reason to not wrap these in
double quotes.</p>
</caption>
<tbody>
<tr>
<td><code>"${array[@]}"</code></td>
<td>Returns every element of the array as a separate token.</td>
</tr><tr>
<td><code>"${array[*]}"</code></td>
<td>Returns every element of the array in a single
whitespace-separated string.</td>
</tr>
</tbody>
</table>
It's really that simple—that covers most usages of arrays, and most of
the mistakes made with them.
To help you understand the difference between `@` and `*`, here is a
sample.
<pre><code>#!/bin/bash
array=(foo bar baz)
for item in "${array[@]}"; do
echo " - <${item}>"
done<hr> - <foo>
- <bar>
- <baz></code></pre>
<pre><code>#!/bin/bash
array=(foo bar baz)
for item in "${array[*]}"; do
echo " - <${item}>"
done<hr> - <foo bar baz></code></pre>
To get individual entries, the syntax is
<code>${array[<var>n</var>]}</code>, where <var>n</var> starts at 0.
<table>
<caption>
<h1>Getting a single entry from an array</h1>
</caption>
<tbody>
<tr>
<td><code>"${array[<var>n</var>]}"</code></td>
<td>Returns the <var>n</var>th entry of the array, where the
first entry is at <var>n</var>=0.</td>
</tr>
</tbody>
</table>
To get a subset of the array, there are a few options (like normal,
switch between `@` and `*` to switch between
getting it as separate items, and as a whitespace-separated string):
<table>
<caption>
<h1>Getting subsets of an array</h1>
<p>Substitute <code>*</code> for <code>@</code> to get the subset
as a whitespace-separated string instead of separate tokens, as
described above.</p>
<p>Again, there is almost no valid reason to not wrap each of
these in double quotes.</p>
</caption>
<tbody>
<tr>
<td><code>"${array[@]:<var>start</var>}"</code></td>
<td>Returns from <var>n</var>=<var>start</var> to the end of the array.</td>
</tr><tr>
<td><code>"${array[@]:<var>start</var>:<var>count</var>}"</code></td>
<td>Returns <var>count</var> entries, starting at <var>n</var>=<var>start</var>.</td>
</tr><tr>
<td><code>"${array[@]::<var>count</var>}"</code></td>
<td>Returns <var>count</var> entries from the beginning of the array.</td>
</tr>
</tbody>
</table>
Notice that `"${array[@]}"` is equivalent to `"${array[@]:0}"`.
<table>
<caption>
<h1>Getting the length of an array</h1>
<p>The is the only situation where there is no difference
between <code>@</code> and <code>*</code>.</p>
</caption>
<tbody>
<tr>
<td>
<code>${#array[@]}</code>
<br>or<br>
<code>${#array[*]}</code>
</td>
<td>
Returns the length of the array
</td>
</tr>
</tbody>
</table>
Argument array syntax
---------------------
Accessing the arguments is mostly that simple, but that array doesn't
actually have a variable name. It's special. Instead, it is exposed
through a series of special variables (normal variables can only start
with letters and underscore), that *mostly* match up with the normal
array syntax.
Setting the arguments array, on the other hand, is pretty different.
That's fine, because setting the arguments array is less useful
anyway.
<table>
<caption>
<h1>Accessing the arguments array</h1>
<aside>Note that for values of <var>n</var> with more than 1
digit, you need to wrap it in <code>{}</code>.
Otherwise, <code>"$10"</code> would be parsed
as <code>"${1}0"</code>.</aside>
</caption>
<tbody>
<tr><th colspan=2>Individual entries</th></tr>
<tr><td><code>${array[0]}</code></td><td><code>$0</code></td></tr>
<tr><td><code>${array[1]}</code></td><td><code>$1</code></td></tr>
<tr><td colspan=2 style="text-align:center">...</td></tr>
<tr><td><code>${array[9]}</code></td><td><code>$9</code></td></tr>
<tr><td><code>${array[10]}</code></td><td><code>${10}</code></td></tr>
<tr><td colspan=2 style="text-align:center">...</td></tr>
<tr><td><code>${array[<var>n</var>]}</code></td><td><code>${<var>n</var>}</code></td></tr>
<tr><th colspan=2>Subset arrays (array)</th></tr>
<tr><td><code>"${array[@]}"</code></td><td><code>"${@:0}"</code></td></tr>
<tr><td><code>"${array[@]:1}"</code></td><td><code>"$@"</code></td></tr>
<tr><td><code>"${array[@]:<var>pos</var>}"</code></td><td><code>"${@:<var>pos</var>}"</code></td></tr>
<tr><td><code>"${array[@]:<var>pos</var>:<var>len</var>}"</code></td><td><code>"${@:<var>pos</var>:<var>len</var>}"</code></td></tr>
<tr><td><code>"${array[@]::<var>len</var>}"</code></td><td><code>"${@::<var>len</var>}"</code></td></tr>
<tr><th colspan=2>Subset arrays (string)</th></tr>
<tr><td><code>"${array[*]}"</code></td><td><code>"${*:0}"</code></td></tr>
<tr><td><code>"${array[*]:1}"</code></td><td><code>"$*"</code></td></tr>
<tr><td><code>"${array[*]:<var>pos</var>}"</code></td><td><code>"${*:<var>pos</var>}"</code></td></tr>
<tr><td><code>"${array[*]:<var>pos</var>:<var>len</var>}"</code></td><td><code>"${*:<var>pos</var>:<var>len</var>}"</code></td></tr>
<tr><td><code>"${array[*]::<var>len</var>}"</code></td><td><code>"${*::<var>len</var>}"</code></td></tr>
<tr><th colspan=2>Array length</th></tr>
<tr><td><code>${#array[@]}</code></td><td><code>$#</code> + 1</td></tr>
<tr><th colspan=2>Setting the array</th></tr>
<tr><td><code>array=("${array[0]}" <var>tokens...</var>)</code></td><td><code>set -- <var>tokens...</var></code></td></tr>
<tr><td><code>array=("${array[0]}" "${array[@]:2}")</code></td><td><code>shift</code></td></tr>
<tr><td><code>array=("${array[0]}" "${array[@]:<var>n+1</var>}")</code></td><td><code>shift <var>n</var></code></td></tr>
</tbody>
</table>
Did notice what was inconsistent? The variables `$*`, `$@`, and `$#`
behave like the <var>n</var>=0 entry doesn't exist.
<table>
<caption>
<h1>Inconsistencies</h1>
</caption>
<tbody>
<tr>
<th colspan=3><code>@</code> or <code>*</code></th>
</tr><tr>
<td><code>"${array[@]}"</code></td>
<td>→</td>
<td><code>"${array[@]:0}"</code></td>
</tr><tr>
<td><code>"${@}"</code></td>
<td>→</td>
<td><code>"${@:1}"</code></td>
</tr><tr>
<th colspan=3><code>#</code></th>
</tr><tr>
<td><code>"${#array[@]}"</code></td>
<td>→</td>
<td>length</td>
</tr><tr>
<td><code>"${#}"</code></td>
<td>→</td>
<td>length-1</td>
</tr>
</tbody>
</table>
These make sense because argument 0 is the name of the script—we
almost never want that when parsing arguments. You'd spend more code
getting the values that it currently gives you.
Now, for an explanation of setting the arguments array. You cannot
set argument <var>n</var>=0. The `set` command is used to manipulate
the arguments passed to Bash after the fact—similarly, you could use
`set -x` to make Bash behave like you ran it as `bash -x`; like most
GNU programs, the `--` tells it to not parse any of the options as
flags. The `shift` command shifts each entry <var>n</var> spots to the
left, using <var>n</var>=1 if no value is specified; and leaving
argument 0 alone.
----
2013-12-06 update: When it's okay to not quote arrays
-----------------------------------------------------
I mentioned that there is "almost no" valid reason to not wrap
`${array[@]}` in double-quotes. Seriously, you probably want to put
it in quotes. In an earlier version, I wrote "no", and in an even
earlier draft I wrote "no reason that I can think of". Well, I just
changed it to "almost no", because I thought of a reason:
It is okay to not quote it when you are doing field-separator
manipulations. For example:
# Usage: path_la PATH1 PATH2...
# Description:
# Takes any number of PATH-style values; that is,
# colon-separated lists of directories, and prints a
# newline-separated list of executables found in them.
# Bugs:
# Does not correctly handle programs with a newline in the name,
# as the output is newline-separated.
path_ls() {
local dirs
IFS=: dirs=($@) # The odd-ball time that it needs to be unquoted
find -L "${dirs[@]}" -maxdepth 1 -type f -executable -printf '%f\n' 2>/dev/null | sort -u
}
The explanation is that no field-separator evaluation is done when you
quote the array. This is almost always what you want—the array is
already field-separated; you don't want that to be re-evaluated, and
possibly changed. However, if you do have character-separated fields
that you want to get at, you do want field-separation to be
re-evaluated.
But seriously, quote your arrays by default. If they need to be
unquoted, you should think long and hard before doing so.
|