How to use the Array Variable in Bash?

Bash Liste Des Attaques Ovh

About

Bash provides a one-dimensional array variables. See also: Bash Shell and (Unix|Linux) Utilities (XCU)

When calling a function, quote the variable otherwise bash will not see the string as atomic. The separator is variable $IFS.

There is:

  • no maximum limit on the size of an array,
  • nor any requirement that members be indexed or assigned contiguously.

Management

Assignment

One value

Implicit

Individual array elements may be assigned to using the

name[subscript]=value

where:

  • subscript is treated as an arithmetic expression that must evaluate to a number greater than or equal to zero. Indexing: Arrays are indexed using integers and are zero-based.
  • name is the name of the array variable
  • value is the value of the variable for name[subscript]
Explicit

The declare builtin will explicitly declare an array.

To explicitly declare an array, use

declare -a name
declare -a name[subscript] # is also accepted but the subscript is ignored

#Example
declare -a arr=("element1" "element2" "element3")

The following builtin command accept a -a option to specify an array

Associative

By default, if not defined, the array is indexed by integer. If you want to have an associative array, you need to declare it

declare -A arr

declare -A name=(key1 value1 key2 value2 … )
declare -A name=( [key1]=value1 [key2]=value2 … )

Multiple values - string word splitting

With IFS being a space, you can transform a string to an array thanks to word splitting

  • Explicit:
string="a b c";
array=( $string )
echo ${array[0]} # a
echo ${array[1]} # b
  • Implicit: The read builtin accepts a -a option to assign a list of words read from the standard input to an array.
# string
IFS=', ' read -r -a array <<< "element1, element2, element3"
# file (every value on one line)
IFS=$'\n' read -r -a array -d $'\c0' < /path/to/file

Multiple values

Arrays are assigned using compound assignments of the form

# Implicit
name=([subscript1]=string1... [subscriptN]=stringN)
array=("element1" "element2" "element3")

# Explicit
declare -a array=("element1" "element2" "element3")
IFS=', ' read -r -a array <<< "element1, element2, element3"

where:

  • Only string is required.
  • If the optional brackets and subscript are supplied, that index is assigned to; otherwise the index of the element assigned is the last index assigned to by the statement plus one. Indexing starts at zero.

This syntax is also accepted by the declare builtin.

Show the array definition with typeset

typeset -p myarray
declare -A myarray=([author]="jon" )

Other

The set and declare builtins display array values in a way that allows them to be reused as assignments.

Append

In the context where an assignment statement is assigning a value to a array index, the += operator, 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.

Example

declare -a myArray
myArray+=(first)
myArray+=(second)

Attributes

Attributes may be specified for an array variable using the:

Each attribute applies to all members of an array.

Reference

Index

Any element of an array may be referenced using

${name[subscript]}

where:

  • subscript is the index (default 0, may be negative). Referencing an array variable without a subscript is equivalent to referencing element zero.
  • The braces are required to avoid conflicts with pathname expansion.

Example with the builtin BASH_VERSINFO array.

echo ${BASH_VERSINFO[0]}
# same as:
echo ${BASH_VERSINFO}
4

Last element

declare -a args=("element1" "element2" "element3")
echo ${args[-1]}
element3

Special subscript @ or *

If subscript is @ or *, the word expands to all members of name.

These subscripts differ only when the word appears within double quotes.

If the word is double-quoted:

  • Array[*] expands to a single word with the value of each array member separated by the first character of the IFS special variable
  • Array[@] expands each element of name to a separate word.

When there are no array members, Array[@] expands to nothing.

If the double-quoted expansion occurs within a word:

  • the expansion of the first parameter is joined with the beginning part of the original word
  • and the expansion of the last parameter is joined with the last part of the original word.

This is analogous to the expansion of the special parameters * and @.

Number of elements

${#name[subscript]} expands to the length of ${name[subscript]}.

This is a parameter expansion mechanism.

If the subscript is * or @, the expansion is the number of elements in the array. Example with the builtin BASH_VERSINFO array.

echo ${#BASH_VERSINFO[*]}
6

List of indices

Through the indices parameter expansion mechanism, it's possible to get a list of indices.

  • Default
name[0]=value0
name[1]=value1
name[3]=value3
echo ${!name[*]}
  • Output
0 1 3

declare -A colors
colors['yellow']=jaune
colors['red']=rouge
echo ${!colors[*]}
yellow red

Slicing

See parameter_expansion substring_expansion

Example with the BASH_VERSINFO, we can slice it to get the element 1 to 4

echo ${BASH_VERSINFO[@]:1:4}
3 46 1 release

Unset (Destroy)

The unset builtin is used to destroy arrays.

To destroy the array element at index subscript.

unset name[subscript] 

Care must be taken to avoid unwanted side effects caused by filename generation.

The following statement removes the entire array.

unset name # where name is an array
# or 
unset name[*]
unset name[@]

Looping

## declare an array variable
IFS=' ' read -r -a array <<< "element1 element2 element3"

## Loop
for i in "${!array[@]}"
do
  printf "$i - ${array[$i]}\n"
done
0 - element1
1 - element2
2 - element3

  • name looping
## declare an array variable
declare -a array=("element1" "element2" "element3")

## 
for name in "${array[@]}"
do
  printf "$name\n"
done
element1
element2
element3

Print / Explode

(IFS=$'\n'; echo "${ARGS[*]}")

example

MY_ARRAY=("element1" "element2" "element3")
(IFS=$'\n'; echo "${MY_ARRAY[*]}")
element1
element2
element3

Contains

To check that an array contains a variable, you can:

  • declare the value as key and check
declare -A myArray=(  [value1]=1  [value2]=1  [value3]=1 )
x=value1
[[ -n "${array1[$x]}" ]]
[[ "${array1[*]}" =~ "value" ]] && echo contains || echo does not contains

Documentation / Reference

man bash





Discover More
Bash Liste Des Attaques Ovh
Bash - (Builtin|Intern|System|Reserved|Shell) variable name

Reserved variable name are named that have a special meaning for the bash shell. PS1 defines the shell's command-line prompt. HOME defines the home directory for a user. PATH defines a list...
Bash Liste Des Attaques Ovh
Bash - (Environment) Variable

A variable is a parameters referenced by a name. parameter A variable has: a value and zero or more attributes (such as integer, ...). Attributes are assigned using the declare builtin command....
Bash Liste Des Attaques Ovh
Bash - Declare (Variable declaration and attribute)

Declare variables and/or give them attributes. where: names: If no names are given then display the values of variables. p will display the attributes and values of each name. When -p is used,...
Bash Liste Des Attaques Ovh
Bash - For Statement

The for statement in bash forloop statements of Bash With a file. Locate returns a list of file in one line separated by a space. This code use then the loop array loop with the Bash version...
Bash Liste Des Attaques Ovh
Bash - How to pass arguments that have space in their values ?

This article shows you how to pass arguments that have space characters in their values. Passing several arguments to a function that are stored as a string may not work properly when you have space...
Bash Liste Des Attaques Ovh
Bash - Parameter Expansion

Parameter expansion are patterns applied to the parameters that have different results. Same as ?? See also: The value of parameter is substituted where: the braces are optional. They served...
Bash Liste Des Attaques Ovh
Bash - Read (Builtin Command) that capture a line

Read is a bash builtin command and read: by default one line or a number of characters (by option) from: the standard input, or from the file descriptor fd supplied as an argument to the...
Bash Liste Des Attaques Ovh
Bash - Readonly Buitlin (Constant variable and function)

The given names are marked readonly; the values of these names may not be changed by subsequent assignment. where: -f set also the functions corresponding to the names read-only -a restricts...
Bash Liste Des Attaques Ovh
Sh - String Variable

String Variable, see also character string in bash. When calling a function, quote the variable otherwise bash will not see the string as atomic but as an array Sh with Bash “” double...
Card Puncher Data Processing
Shell - (Word | Token | Field)

In a bash pipeline, the pipeline message is known as: the word the field This is a sequence of characters considered as a single unit by the shell. The first message is the standard input....



Share this page:
Follow us:
Task Runner