Bash : arrays

bash shell


Bash arrays may be useful to manage certain amount of arranged or random data, here is a quick reminder of possibles operations.




1) Generalities

Within an array context expansion rules are the same as for a bash variable, also the numeric values used for array as for the element number, the expansion offset and length can be any arithmetic expression.

For example the following statements are equivalent:

echo ${array[2]} # gives the element number 2 of the array
echo ${array[((4 - 2))]} # gives the element number 2 of the array
echo ${array[((8 / 4))]} # gives the element number 2 of the array

Within array context offset start at 0 (not 1!)


2) Declaration / deletion of arrays

  • Declare the array
    declare -a array

    or a simple

    array=( elem1 elem2 elem3 )
  • Append element(s) to an existing array
    existing-array=( ${existing-array[*]} added1 added2 last-added )
  • Append an existing array to a newly created one
    new-array=( "${existing-array[@]}" )
  • Delete an entire array
    unset array

    Note : notice that we don’t use the classic array notation here.


3) Operations on arrays

In the next following examples we will use this array:

array=( item1 item2 item3 )

Note : I often had problem when using arrays in ubuntu, because of the dash shell (more info here).

  • Get the whole of an array as a list of individual elements (words):
    echo ${array[*]} # gives: item1 item2 item3


    echo ${array[@]} # gives: item1 item2 item3
  • Get the whole of an array as one word:
    echo "${array[*]}" # gives: "item1 item2 item3"
  • Get the whole of an array as each array elements individually double-quoted:
    echo "${array[@]}" # gives: "item1" "item2" "item3"


4) Operations on elements

  • Get the first element of an array (element start at “0”)
    echo "${array[0]}" # gives: item1
  • Get “M” element(s) from the element n° “N” (“N” is the “offset” and “M” is the number of elements), in this example we use “N“=1 and “M“=2
    echo ${array[*]:1:2} # gives: item2 item3
  • Get “M” character(s) from the element (n°”E“) character n° “N” (“N” is the “offset“, “M” is the number of characters to be extracted from element n°”E“), in this example we use “E“=0 ,”N“=1 and “M“=2 (which represent 2 characters starting at character 1 of element number 0)

    echo ${array[0]:1:2} # gives: te
  • Get “M” character(s) starting from the end of element (n°”E“), in this example we use “E“=0 and “M“=2 (in this case the “length” is omitted so the only given value is taken as an “offset“, the expansion then start at the offsetposition up to the end of element)
    echo ${array[0]:2} # gives: em1
  • The last element of ANY array, using a function
    In this example
    the key command is the second to last one – LastElem=${!#} – it first use the “#” which stores the “total number of arguments” and then using the indirection mark “!” it aims to the argument value itself: the last element.

    # this function provides the last element of the given array
    #+ array must be given as ${array[*]}
    GetLastElem() {
    declare -a array
    # set the function parameters as the array content
    array=( $@ )
    echo ${LastElem}
  • Remove an array element part from the beginning of each elements matching the given pattern (where <pattern> could be any BREpattern)
    echo ${array[@]#item} # gives 1 2 3
  • Remove an array element part from the end of each elements matching the given pattern (where <pattern> could be any BREpattern)
    echo echo ${array[@]#item} # gives item item item

    Note : see the “Bash : operators, expansions & special variables” for more infos about parameters expansion (for variable and array)

  • Create a “dynamic” array name (using indirect references and eval command)
    # This function is only used to show the implementation of dynamic array names in bash
    # Usage: Call this function with an "existing array" name as first parameter
    #+ the array name should be given without any special notation, see example below
    # Example : $0 "array_name"
    DynamicArrayName () {
    param_array="$1"                                    # this is the array given as parameter to the function
    func_array=( $(eval echo \${${param_array}[@]}) )   # the "given array" elements are now appended to "func_array", the one use inside the function
    echo "array name given as param (\$param_array) was : $param_array"
    echo "array name used in function (\$func_array) is : $func_array"
    echo "\$func_array -should contains every elements from \$param_array- contains : $(echo ${func_array[@]})"



Tagged on: , , ,

Leave a Reply

Your email address will not be published. Required fields are marked *

This site supports SyntaxHighlighter via WP SyntaxHighlighter. It can highlight your code.
How to highlight your code: Paste your code in the comment form, select it and then click the language link button below. This will wrap your code in a <pre> tag and format it when submitted.