Reputation: 145107
Is there a way to do something like PHPs $array[] = 'foo';
in bash vs doing:
array[0]='foo'
array[1]='bar'
Upvotes: 1024
Views: 878266
Reputation: 360565
As Dumb Guy points out, it's important to note whether the array starts at zero and is sequential. Since you can make assignments to and unset non-contiguous indices ${#array[@]}
is not always the next item at the end of the array.
$ array=(a b c d e f g h)
$ array[42]="i"
$ unset array[2]
$ unset array[3]
$ declare -p array # dump the array so we can see what it contains
declare -a array='([0]="a" [1]="b" [4]="e" [5]="f" [6]="g" [7]="h" [42]="i")'
$ echo ${#array[@]}
7
$ echo ${array[${#array[@]}]}
h
Here's how to get the last index:
$ end=(${!array[@]}) # put all the indices in an array
$ end=${end[@]: -1} # get the last one
$ echo $end
42
That illustrates how to get the last element of an array. You'll often see this:
$ echo ${array[${#array[@]} - 1]}
g
As you can see, because we're dealing with a sparse array, this isn't the last element. This works on both sparse and contiguous arrays, though:
$ echo ${array[@]: -1}
i
Upvotes: 102
Reputation: 199
if you are using variables, rather than direct inserting the value (hard coded). make sure to add double quote.
below code works for single word or integers, but failed for multi words strings
array=()
avar=1
array+=($avar)
you need to add quote around the $avar
array=()
avar="long long words"
array+=("$avar")
Upvotes: 2
Reputation: 12748
also check this out :
test_array=(1 2 3 4)
test_array+=(5)
echo "${test_array[@]}"
Result : 1 2 3 4 5
the +=
operator can be used to append a single element or multiple elements to an array, in instance, you can append more elements to the array like this:
test_array+=(6 7 8 9)
echo "${test_array[@]}"
Result : 1 2 3 4 5 6 7 8 9
I wanna mention that you can also remove an element from an array in Bash without specifying the index, you can use the unset
command with the element's value
test_array=(1 2 3 4 5)
unset test_array[2]
echo "${test_array[@]}"
Result : 1 2 4 5
be careful, when you remove an element from an array, the indices of the remaining elements will be adjusted accordingly. In the example above, when I remove the third element with the value 3
, the fourth and fifth elements become the new third and fourth elements, respectively.
Upvotes: 6
Reputation: 1249
Append element:
array+=("${element}")
Append another array:
array+=("${array[@]}")
Append command output:
readarray -t output < <(command)
array+=("${output[@]}")
Upvotes: 1
Reputation: 25351
Yes there is:
ARRAY=()
ARRAY+=('foo')
ARRAY+=('bar')
In the context where an assignment statement is assigning a value to a shell variable or array index (see Arrays), the ‘+=’ operator can be used to append to or add to the variable's previous value.
Also:
When += is applied to an array variable using compound assignment (see Arrays below), the variable's value is not unset (as it is when using =), and new values are appended to the array beginning at one greater than the array's maximum index (for indexed arrays)
Upvotes: 1971
Reputation: 3446
If your array is always sequential and starts at 0, then you can do this:
array[${#array[@]}]='foo'
# gets the length of the array
${#array_name[@]}
If you inadvertently use spaces between the equal sign:
array[${#array[@]}] = 'foo'
Then you will receive an error similar to:
array_name[3]: command not found
Upvotes: 39
Reputation: 172
With an indexed array, you can to something like this:
declare -a a=()
a+=('foo' 'bar')
Upvotes: 11
Reputation: 342889
$ declare -a arr
$ arr=("a")
$ arr=("${arr[@]}" "new")
$ echo ${arr[@]}
a new
$ arr=("${arr[@]}" "newest")
$ echo ${arr[@]}
a new newest
Upvotes: 72