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
-------------------
Setting an array
tokens... is expanded and split into array elements
the same way command line arguments are.
array=(tokens...)
Set the contents of the entire array.
array+=(tokens...)
Appends tokens... to the end of the array.
array[n]=value
Sets an individual entry in the array, the first entry is at
n=0.
Now, for accessing the array. The most important things to
understanding arrays is to quote them, and understanding the
difference between `@` and `*`.
Getting an entire array
There is almost no valid reason to not wrap these in
double quotes.
"${array[@]}"
Returns every element of the array as a separate token.
"${array[*]}"
Returns every element of the array in a single
whitespace-separated string.
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.
#!/bin/bash
array=(foo bar baz)
for item in "${array[@]}"; do
echo " - <${item}>"
done - <foo>
- <bar>
- <baz>
#!/bin/bash
array=(foo bar baz)
for item in "${array[*]}"; do
echo " - <${item}>"
done - <foo bar baz>
To get individual entries, the syntax is
${array[n]}, where n starts at 0.
Getting a single entry from an array
"${array[n]}"
Returns the nth entry of the array, where the
first entry is at n=0.
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):
Getting subsets of an array
Substitute * for @ to get the subset
as a whitespace-separated string instead of separate tokens, as
described above.
Again, there is almost no valid reason to not wrap each of
these in double quotes.
"${array[@]:start}"
Returns from n=start to the end of the array.
"${array[@]:start:count}"
Returns count entries, starting at n=start.
"${array[@]::count}"
Returns count entries from the beginning of the array.
Notice that `"${array[@]}"` is equivalent to `"${array[@]:0}"`.
Getting the length of an array
The is the only situation where there is no difference
between @ and *.
${#array[@]} or ${#array[*]}
Returns the length of the array
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.
Accessing the arguments array
Individual entries
${array[0]}
$0
${array[1]}
$1
...
${array[9]}
$9
${array[10]}
${10}
...
${array[n]}
${n}
Subset arrays (array)
"${array[@]}"
"${@:0}"
"${array[@]:1}"
"$@"
"${array[@]:pos}"
"${@:pos}"
"${array[@]:pos:len}"
"${@:pos:len}"
"${array[@]::len}"
"${@::len}"
Subset arrays (string)
"${array[*]}"
"${*:0}"
"${array[*]:1}"
"$*"
"${array[*]:pos}"
"${*:pos}"
"${array[*]:pos:len}"
"${*:pos:len}"
"${array[*]::len}"
"${*::len}"
Array length
${#array[@]}
$# + 1
Setting the array
array=("${array[0]}" tokens...)
set -- tokens...
array=("${array[0]}" "${array[@]:2}")
shift
array=("${array[0]}" "${array[@]:n+1}")
shift n
Did notice what was inconsistent? The variables `$*`, `$@`, and `$#`
behave like the n=0 entry doesn't exist.
Inconsistencies
@ or *
"${array[@]}"
→
"${array[@]:0}"
"${@}"
→
"${@:1}"
#
"${#array[@]}"
→
length
"${#}"
→
length-1
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 n=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 n spots to the
left, using n=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.