Bash


Arrays All Versions

0.99
1.01
2.0
3.0
3.1
3.2
4.0
4.1
4.2
4.3
4.4

This draft deletes the entire topic.

Introduction

Introduction

expand all collapse all

Examples

  • 17

    List Assignment

    Create an array with new elements:

    array=('first element' 'second element' 'third element')
    

    Subscript Assignment

    Create an array with explicit element indices:

    array=([3]='fourth element' [4]='fifth element')
    

    Assignment by index

    array[0]='first element'
    array[1]='second element'
    

    Assignment by name (associative array)

    declare -A array
    array[first]='First element'
    array[second]='Second element'
    

    Dynamic Assignment

    Create an array from the output of other command, for example use seq to get a range from 1 to 10:

    array=(`seq 1 10`)
    

    Assignment from script's input arguments:

    array=("$@")
    

    Assignment within loops:

    while read -r; do
        #array+=("$REPLY")     # Array append
        array[$i]="$REPLY"     # Assignment by index
        let i++                # Increment index 
    done < <(seq 1 10)  # command substitution
    echo ${array[@]}    # output: 1 2 3 4 5 6 7 8 9 10
    

    where $REPLY is always the current input

  • 10

    Print element at index 0

    echo "${array[0]}"
    

    Print last element (available from Bash 4.3)

    echo "${array[-1]}"
    

    Print all elements

    echo "${array[@]}"
    

    Print all elements as a single string

    echo "${array[*]}"
    

    Print all elements from index 1

    echo "${array[@]:1}"
    

    Print 3 elements from index 1

    echo "${array[@]:1:3}"
    

    String Operations

    If referring to a single element, string-operations are permitted:

    array=(zero one two)
    echo ${array[0]:0:3} # gives out zer (chars at position 0, 1 and 2 in the string zero)
    echo ${array[0]:1:3} # gives out ero (chars at position 1, 2 and 3 in the string zero)
    

    so ${array[$i]:N:M} gives out a string from the N'th position (starting from 0) in the string ${array[$i]} with M following chars

  • 9

    Change Index

    Initialize or update a particular element in the array

    array[10]="elevenths element"    # because it's starting with 0
    

    Append

    Modify array, adding elements to the end if no subscript is specified.

    array+=('fourth element' 'fifth element')
    

    Replace the entire array with a new parameter list.

    array=("${array[@]}" "fourth element" "fifth element")
    

    Add an element at the beginning:

    array=("new element" "${array[@]}")
    

    Insert

    Insert an element at a given index:

    arr=(a b c d)
    # insert an element at index 2
    i=2
    arr=("${arr[@]:0:$i}" 'new' "${arr[@]:$i}")
    echo "${arr[2]}" #output: new
    

    Delete

    Delete array indexes using the unset builtin:

    arr=(a b c)
    echo "${arr[@]}"   # outputs: a b c
    echo "${!arr[@]}"  # outputs: 0 1 2
    unset -v 'arr[1]'
    echo "${arr[@]}"   # outputs: a c
    echo "${!arr[@]}"  # outputs: 0 2
    

    Re-index an array

    This can be useful if elements have been removed from an array, or if you're unsure whether there are gaps in the array. To recreates the indices:

    array=("${array[@]}")
    
Please consider making a request to improve this example.

Syntax

Syntax

Parameters

Parameters

Remarks

Remarks

Still have a question about Arrays? Ask Question

Topic Outline