Bash Arrays: Difference between revisions

From NovaOrdis Knowledge Base
Jump to navigation Jump to search
No edit summary
Line 10: Line 10:
* http://tldp.org/LDP/abs/html/arrays.html
* http://tldp.org/LDP/abs/html/arrays.html


==Overview==
==Indexed Arrays Overview==


bash indexed arrays are 0-based and unidimensional. No explicit declaration is necessary if at least one element is [[Bash_Arrays#Assign_Individual_Elements|initialized]] as described below:
bash indexed arrays are 0-based and unidimensional. No explicit declaration is necessary if at least one element is [[Bash_Arrays#Assign_Individual_Elements|initialized]] as described below:


==Declaration==
==Indexed Arrays Declaration==


Arrays do not need explicit declarations, they will be automatically declared upon the first assignment of any of their elements:
Arrays do not need explicit declarations, they will be automatically declared upon the first assignment of any of their elements:
Line 38: Line 38:
A subscript of "*" or "@" also removes the entire array.
A subscript of "*" or "@" also removes the entire array.


==Assignment==
==Indexed Arrays Assignment==


===Assign Individual Elements===
===Assign Indexed Array Individual Elements===


Individual elements are initialized by using ${''var-name''[''key'']}=''value'' notation:
Individual elements are initialized by using ${''var-name''[''key'']}=''value'' notation:
Line 46: Line 46:
  a["color"]="blue"
  a["color"]="blue"


===Entire Array===
===Entire Indexed Array===


The (["key"]="value ...) syntax can be used to initialize an entire array. The values must be separated by space:
The (["key"]="value ...) syntax can be used to initialize an entire array. The values must be separated by space:
Line 52: Line 52:
  a=(["color]="blue" ["size"]="large")
  a=(["color]="blue" ["size"]="large")


==Reference==
==Indexed Arrays Reference==


===Reference Individual Elements===
===Reference Individual Indexed Array Elements===


Element arrays can be referenced with ${''var-name''[''key'']} notation:
Element arrays can be referenced with ${''var-name''[''key'']} notation:
Line 62: Line 62:
If the array variable [[Bash_Variables#Undeclared_Variable|was not previously declared]], or if the specific element [[Bash_Variables#Uninitialized_Variable|was not initialized]], a reference attempt will return the blank string.
If the array variable [[Bash_Variables#Undeclared_Variable|was not previously declared]], or if the specific element [[Bash_Variables#Uninitialized_Variable|was not initialized]], a reference attempt will return the blank string.


===Reference All Elements===
===Reference All Indexed Array Elements===


If '@' or '*' is used as subscript, the following expressions expand to all members of the array:
If '@' or '*' is used as subscript, the following expressions expand to all members of the array:
Line 69: Line 69:
  ${var_name[*]}
  ${var_name[*]}


==Length==
==Length of an Indexed Array==


  ${#var_name[@]}
  ${#var_name[@]}
Line 76: Line 76:
{{Warn|If the array is sparse, this does not return the index of last element plus one, but just the number of non-empty elements.}}
{{Warn|If the array is sparse, this does not return the index of last element plus one, but just the number of non-empty elements.}}


==Iterate==
==Iterate over an Indexed Array==


===Using the Elements===
===Using the Indexed Array Elements===


  for i in ${array_var[@]}; do
  for i in ${array_var[@]}; do
Line 94: Line 94:
Note that this does not work when the array is sparse, because ${#array_var[@]} only returns the number of non-empty elements, and not the array length.
Note that this does not work when the array is sparse, because ${#array_var[@]} only returns the number of non-empty elements, and not the array length.


==Use Cases==
==Indexed Array Use Cases==


===Read the Content of a File into An Array===
===Read the Content of a File into An Indexed Array===


Each line will be assigned to an element of the array:
Each line will be assigned to an element of the array:
Line 102: Line 102:
  readarray < ./somefile.txt -t my_array_var
  readarray < ./somefile.txt -t my_array_var


===Assign Words Read from stdin to an Array===
===Assign Words Read from stdin to an Indexed Array===


Assing words read from stdin to an array:
Assing words read from stdin to an array:
Line 117: Line 117:
=Associative Arrays=
=Associative Arrays=


==Overview==
==Associative Array Overview==


Associative arrays can be used to implement sets and maps in bash. The most common usage as set is to insert elements whose subscript is identical with the value.
Associative arrays can be used to implement sets and maps in bash. The most common usage as set is to insert elements whose subscript is identical with the value.


==Declaration==
==Associative Array Declaration==


{{Warn|Associative arrays '''must''' be explicitly declare before initialization, otherwise unexpected behavior may occur. This is different from the behavior of the indexed arrays, which can be used without explicit declaration.}}
{{Warn|Associative arrays '''must''' be explicitly declare before initialization, otherwise unexpected behavior may occur. This is different from the behavior of the indexed arrays, which can be used without explicit declaration.}}
Line 135: Line 135:
  unset my_assoc_array
  unset my_assoc_array


==Assignment==
==Associative Array Assignment==


===Assign Individual Elements===
===Assign Individual Associative Array Elements===


Individual elements are initialized by using ${''var-name''[''key'']}=''value'' notation:
Individual elements are initialized by using ${''var-name''[''key'']}=''value'' notation:
Line 143: Line 143:
  a["color"]="blue"
  a["color"]="blue"


===Entire Array===
===Entire Associative Array===


The (...) syntax can be used to initialize an entire array. The values must be separated by space:
The (...) syntax can be used to initialize an entire array. The values must be separated by space:
Line 153: Line 153:
  a=([0]="X" [4]="Y" [8]="Z")
  a=([0]="X" [4]="Y" [8]="Z")


==Reference==
==Associative Array Reference==


===Reference Individual Elements===
===Reference Individual Associative Array Elements===


Element arrays can be referenced with ${''var-name''[''index'']} notation:
Element arrays can be referenced with ${''var-name''[''index'']} notation:
Line 163: Line 163:
If the array variable [[Bash_Variables#Undeclared_Variable|was not previously declared]], or if the specific element [[Bash_Variables#Uninitialized_Variable|was not initialized]], a reference attempt will return the blank string.
If the array variable [[Bash_Variables#Undeclared_Variable|was not previously declared]], or if the specific element [[Bash_Variables#Uninitialized_Variable|was not initialized]], a reference attempt will return the blank string.


===Reference All Elements===
===Reference All Associative Array Elements===


If '@' or '*' is used as subscript, the following expressions expand to all members of the array:
If '@' or '*' is used as subscript, the following expressions expand to all members of the array:

Revision as of 23:16, 23 February 2018

Internal

Indexed Arrays

External

Indexed Arrays Overview

bash indexed arrays are 0-based and unidimensional. No explicit declaration is necessary if at least one element is initialized as described below:

Indexed Arrays Declaration

Arrays do not need explicit declarations, they will be automatically declared upon the first assignment of any of their elements:

my_array_var[0]="something"

They can be explicitly declared, though:

declare -a my_array_var

Previously declared arrays can be listed with:

declare -a

An array variable can be "undeclared" with the "unset" builtin.

unset my_array_var

Individual array elements can also be unset:

unset my_array_var[subscript] 

A subscript of "*" or "@" also removes the entire array.

Indexed Arrays Assignment

Assign Indexed Array Individual Elements

Individual elements are initialized by using ${var-name[key]}=value notation:

a["color"]="blue"

Entire Indexed Array

The (["key"]="value ...) syntax can be used to initialize an entire array. The values must be separated by space:

a=(["color]="blue" ["size"]="large")

Indexed Arrays Reference

Reference Individual Indexed Array Elements

Element arrays can be referenced with ${var-name[key]} notation:

echo ${a["color"]}

If the array variable was not previously declared, or if the specific element was not initialized, a reference attempt will return the blank string.

Reference All Indexed Array Elements

If '@' or '*' is used as subscript, the following expressions expand to all members of the array:

${var_name[@]}
${var_name[*]}

Length of an Indexed Array

${#var_name[@]}
${#var_name[*]}

If the array is sparse, this does not return the index of last element plus one, but just the number of non-empty elements.

Iterate over an Indexed Array

Using the Indexed Array Elements

for i in ${array_var[@]}; do
   ...
done

Using the Index

i=0
while [[ ${i} < ${#array_var[@]} ]]; do
  echo ${array_var[$i]}
  ((i++))
done

Note that this does not work when the array is sparse, because ${#array_var[@]} only returns the number of non-empty elements, and not the array length.

Indexed Array Use Cases

Read the Content of a File into An Indexed Array

Each line will be assigned to an element of the array:

readarray < ./somefile.txt -t my_array_var

Assign Words Read from stdin to an Indexed Array

Assing words read from stdin to an array:

read -a

Iterate Over the Argument List

args=("$@")
for arg in ${args[@]}; do
  ...
done

Associative Arrays

Associative Array Overview

Associative arrays can be used to implement sets and maps in bash. The most common usage as set is to insert elements whose subscript is identical with the value.

Associative Array Declaration


Associative arrays must be explicitly declare before initialization, otherwise unexpected behavior may occur. This is different from the behavior of the indexed arrays, which can be used without explicit declaration.

declare -A my_assoc_array

Previously declared associative arrays can be listed with:

declare -A

An associative array variable can be "undeclared" with the "unset" builtin.

unset my_assoc_array

Associative Array Assignment

Assign Individual Associative Array Elements

Individual elements are initialized by using ${var-name[key]}=value notation:

a["color"]="blue"

Entire Associative Array

The (...) syntax can be used to initialize an entire array. The values must be separated by space:

a=("X" "Y" "Z")

If we want initialize only specific elements, we can use this syntax:

a=([0]="X" [4]="Y" [8]="Z")

Associative Array Reference

Reference Individual Associative Array Elements

Element arrays can be referenced with ${var-name[index]} notation:

echo ${a[0]}

If the array variable was not previously declared, or if the specific element was not initialized, a reference attempt will return the blank string.

Reference All Associative Array Elements

If '@' or '*' is used as subscript, the following expressions expand to all members of the array:

${var_name[@]}
${var_name[*]}

Length

${#var_name[@]}
${#var_name[*]}

If the array is sparse, this does not return the index of last element plus one, but just the number of non-empty elements.

Iterate

Using the Elements

for i in ${array_var[@]}; do
   ...
done

Using the Index

i=0
while [[ ${i} < ${#array_var[@]} ]]; do
  echo ${array_var[$i]}
  ((i++))
done

Note that this does not work when the array is sparse, because ${#array_var[@]} only returns the number of non-empty elements, and not the array length.

Use Cases

Read the Content of a File into An Array

Each line will be assigned to an element of the array:

readarray < ./somefile.txt -t my_array_var

Assign Words Read from stdin to an Array

Assing words read from stdin to an array:

read -a

Iterate Over the Argument List

args=("$@")
for arg in ${args[@]}; do
  ...
done