Bash array

From thelinuxwiki
Revision as of 19:01, 25 February 2013 by Nighthawk (Talk | contribs)

(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to: navigation, search

Arrays do not to be declared. Bash allows on dimensional arrays.

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[@]}