Difference between revisions of "Bash array"
From thelinuxwiki
				
								
				
				
																
				
				
								
				| (2 intermediate revisions by one user not shown) | |||
| Line 4: | Line 4: | ||
| == Accessing arrays == | == Accessing arrays == | ||
| − | where  | + | where array = array name | 
| + | |||
| + |   ${array[*]}         # All of the items in the array (single word) | ||
| + |   ${array[@]}         # All items of array (separate words, add quotes if words have spaces) | ||
| + |   ${!array[*]}        # All of the indexes in the array | ||
| + |   ${#array[*]}        #Number of items in the array | ||
| + |   or ${#array[@]}  ??? | ||
| + |   ${#array[0]}        # Length of item zero | ||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| [http://www.linuxjournal.com/content/bash-arrays more info] | [http://www.linuxjournal.com/content/bash-arrays more info] | ||
| Line 46: | Line 48: | ||
|    test_function # the function msut be invoked below or after it is defined |    test_function # the function msut be invoked below or after it is defined | ||
|    echo "original_array = ${color[@]}" |    echo "original_array = ${color[@]}" | ||
| − | |||
| ==  List all elements of original array. == | ==  List all elements of original array. == | ||
|    echo "original_array = ${original_array[@]}" |    echo "original_array = ${original_array[@]}" | ||
| − | |||
| == Get array length == | == Get array length == | ||
|   echo ${#distro[@]} |   echo ${#distro[@]} | ||
| + | |||
| + | == loop / iterate through array == | ||
| + |  for i in ${array-name[@]} | ||
| + |  do | ||
| + |    print $i | ||
| + |  done | ||
| + | |||
| [[category:bash]] | [[category:bash]] | ||
Latest revision as of 19:53, 2 October 2013
Arrays do not to be declared. Bash allows on dimensional arrays.
| Contents | 
Accessing arrays
where array = array name
 ${array[*]}         # All of the items in the array (single word)
 ${array[@]}         # All items of array (separate words, add quotes if words have spaces)
 ${!array[*]}        # All of the indexes in the array
 ${#array[*]}        #Number of items in the array
 or ${#array[@]}  ???
 ${#array[0]}        # Length of item zero
Example Array 1:
 #!/bin/bash
 function test_function {
   color[1]=red           # <<< array assignments
   color[2]=white         # <<< array assignments
   color[3]=blue          # <<< array assignments
   # Using brace expansion ...
   # Bash, version 3+.
   for a in {1..3}
   do
     echo "${color[$a]}"    #<<<  curly braces needed !!!!
   done
 }
 #main body
 test_function # the function msut be invoked below or after it is defined
Example Array 2
 #!/bin/bash
 function test_function {
   color=( zero one two three )           # <<< array assignments
    # Using brace expansion ...
       # Bash, version 3+.
  for a in {1..3}
     do
          echo "${color[$a]}"    #<<<  curly braces needed !!!!
     done
      }
 #main body
 test_function # the function msut be invoked below or after it is defined
 echo "original_array = ${color[@]}"
List all elements of original array.
 echo "original_array = ${original_array[@]}"
Get array length
echo ${#distro[@]}
        
loop / iterate through array
for i in ${array-name[@]}
do
  print $i
done
 
					