Programming
Bash
Arrays
Initialise
Insert
Overwrite
Count
Retrieve by index
Remove by index
Slice
Iterate
Search
Regex - grep
Strings
C
Arrays
Regex - POSIX
Strings
C++
Arrays
Bimaps
Functions
Maps
Regex
Strings
Go
Arrays
Maps
Strings
Java
Arrays
Regex
Strings
JavaScript
Arrays
Functions
Objects
Regex
Strings
Perl
Arrays
Regex
Strings
Python
Arrays
Functions
Regex
Strings
Bash Arrays
alternatives colour
Initialise
Fixed contents n/a a=(1 2 3 4 5)
1 2 3 4 5
Initialise from CR-terminated list l="one
two
three"
a=($l)
one two three
Insert
Prepend element 'e' a=(2 3 4)
e=1;
a=($e ${a[*]})
1 2 3 4
Append element 'e' a=(1 2 3)
e=4;
a=(${a[*]} $e)
1 2 3 4
Insert element 'e' at index 'n' a=(1 2 3 4 5)
e=666
n=3
a=(${a[*]:0:$n} $e ${a[*]:$n})
1 2 3 666 4 5
Prepend array 'b' a=(1 2 3 4 5)
b=(10 20 30)
a=(${b[*]} ${a[*]})
10 20 30 1 2 3 4 5
Append array 'b' a=(1 2 3 4 5)
b=(10 20 30)
a=(${a[*]} ${b[*]})
1 2 3 4 5 10 20 30
Insert array 'b' at index 'n' a=(1 2 3 4 5)
b=(10 20 30)
n=3;
a=(${a[*]:0:$n} ${b[*]} ${a[*]:$n})
1 2 3 10 20 30 4 5
Concatenate array 'a' and array 'b' a=(1 2 3)
b=(4 5 6)
c=(${a[*]} ${b[*]})
1 2 3 4 5 6
Overwrite
Overwrite first element a=(1 2 3)
a[0]=666
666 2 3
Overwrite last element a=(1 2 3)
a[${#a[*]}-1]=666
1 2 666
Overwrite element at index 'n' a=(1 2 3)
n=1
a[n]=666
1 666 3
Count
Length of array a=(1 2 3)
n=${#a[*]}
3
Maximum element in array a=(1 2 300 4 5)
{ n=$(tail -n1); } < <(printf "%s\n" "${a[@]}" | sort -n)
300
Minimum element in array a=(10 20 3 40 50)
{ read n; } < <(printf "%s\n" "${a[@]}" | sort -n)
3
Retrieve by index
Retrieve first element a=(1 2 3)
e=${a[0]}
1
Retrieve last element a=(1 2 3)
e=${a[*]: -1}
3
Retrieve last element [alternative] a=(1 2 3)
e=${a[${#a[*]}-1]}
3
Retrieve element 'n' a=(1 2 3)
n=2
e=${a[$n]}
3
Remove by index
Remove element at index 'n' a=(1 2 3 4 5)
n=2;
unset a[n];//output
1 2 4 5
Remove elements in index range 'm' to 'n' (inclusive) a=(1 2 3 4 5)
m=1;
n=3;
a=(${a[@]:0:$m} ${a[@]:$(($n + 1))})
1 5
Slice
Slice from index 'm' to 'n' (inclusive) a=(1 2 3 4 5)
m=1
n=3
a=("${a[@]:$m:$n}")
2 3 4
Slice from index 'n' onwards (inclusive) a=(1 2 3 4 5)
n=2
a=("${a[@]:$n}")
3 4 5
Slice up to index 'n' (inclusive) a=(1 2 3 4 5)
n=3;
a=("${a[@]::$n+1}")
1 2 3 4
Slice up to 'n' elements from end a=(1 2 3 4 5)
n=2
i=${#a[*]}-$n
a=("${a[@]::$i}")
1 2 3
Iterate
Iterate over array 'a' a=(1 2 3 4 5)
b=()
for n in ${a[@]}; do
  let "m = n * 2"
  b=(${b[*]} $m)
done
2 4 6 8 10
Search
Find and replace all instances of 'x' with 'y' a=(1 2 666 3 4 666 5)
x=666
y=999
a=(${a[@]/${x}/${y}})
1 2 999 3 4 999 5