blob: 485671690fdc90fb067e768103af8fe7a0b321f1 (
plain) (
blame)
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
|
GNU `bash`
==========
(Associative) arrays
--------------------
### Declaration
`"${#xs[@]}"` doesn't work with `nounset` if `xs` wasn't defined, i.e. was
declared with either of
local -a xs
declare -a xs
local -A xs
declare -A xs
Therefore, if you want to extract the length of an array, append `=()` to the
statements above.
local -a xs=()
declare -a xs=()
...
And now `"${#xs[@]}"` works with `nounset`.
It doesn't affect expansion (see below) though.
### Expansion
#### Do
func ${arr[@]+"${arr[@]}"}
#### Don't
func "${arr[@]}" # Doesn't work with `nounset`.
func "${arr[@]+"${arr[@]}"}" # Doesn't work properly with `declare -a arr=('')`.
### `unset`
#### Do
unset -v 'arr[x]'
unset -v 'arr[$i]'
#### Don't
unset -v arr[x] # May break due to globbing.
unset -v arr[$i] # The same as above + a possible problem with quotation.
unset -v 'arr["x"]' # Doesn't work for some reason.
unset -v 'arr["]"]' # The same as above; just highlighting the problem with funny characters in array indices.
unset -v 'arr["$i"]' # Also rejected.
# An insightful discussion on the topic: https://lists.gnu.org/archive/html/help-bash/2016-09/msg00020.html.
`errexit`
---------
### Do
bar_output="$( bar )"
foo "$bar_output"
### Don't
foo "$( bar )" # With `errexit`, foo will still get executed.
# I don't know why.
|