Linux Command Reference ยท Bash Syntax
Bash indexed arrays
Reference for Bash indexed arrays: create them, access elements and slices, get length and indices, append, delete, copy and loop over elements safely.
An indexed array is a variable that holds a numbered list of strings. Indices start at 0, may be sparse (gaps are allowed), and the array grows as you assign. Arrays are how Bash keeps a list of items, such as file names, without the problems of splitting a space-separated string.
Expansion syntax decides what you get: "${arr[@]}" gives each element as a separate word, "${arr[*]}" joins them into one word, and ${#arr[@]} gives the count.
arr=(one two three) echo "${arr[1]}" # two echo "${#arr[@]}" # 3
Try it
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with read or cat), it gets the next line from this box.
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with read or cat), it gets the next line from this box.
Syntax
name=(value1 value2 ...) name[index]=value name+=(value ...) ${name[index]} ${name[@]} ${name[*]} ${#name[@]} ${!name[@]} ${name[@]:offset:length} unset name[index] unset name
| Expression | Description |
|---|---|
${arr[i]} |
Element i; negative indices count from the end (Bash 4.3+). |
"${arr[@]}" |
All elements, each as a separate word. |
"${arr[*]}" |
All elements as one word, joined by the first character of IFS. |
${#arr[@]} |
Number of set elements. |
${#arr[i]} |
Length of element i. |
${!arr[@]} |
The list of used indices. |
${arr[@]:o:n} |
Slice of n elements starting at offset o. |
arr+=(...) |
Append elements. |
declare -a arr |
Declare an indexed array explicitly. |
Edge cases: a plain $arr is the same as ${arr[0]}; a missing index expands to empty; unquoted ${arr[@]} splits elements on whitespace and expands globs.
Usage notes
- Always quote
"${arr[@]}"when looping or passing elements on; otherwise elements with spaces are split. - Indices need not be consecutive. After
unset 'arr[1]'the others keep their numbers, and${#arr[@]}counts only the remaining elements. To close the gaps, copy:arr=("${arr[@]}"). - Quote the argument to
unset(unset 'a[1]') so the shell does not treat[1]as a glob. - Assigning a string to an array name (
arr=hello) sets element 0 only. - Arrays cannot be exported to other programs or nested; for key-value data use associative arrays.
mapfile -t(alsoreadarray) reads lines into an array in one step and is safer thanarr=($(cmd)), which splits on whitespace and expands globs.- Negative indices count from the end only in Bash 4.3+. The slice form
${arr[@]: -2}needs the space before the minus sign. - Indexed arrays are Bash/ksh/zsh features (zsh starts at index 1); they do not exist in POSIX
sh.
Related
- Associative arrays
- for loops
- Functions
- Tutorial: Arrays