Arrays in Shell

830 阅读1分钟
原文链接: aleen42.gitbooks.io

Arrays may be initialized with variable[n] notation, and to access elements with curly bracket notation like $[variable[n]].

Initialization


base64_charset=( {A..Z} {a..z} {0..9} + / = );

base64_charset=( [0]={A..Z} [1]={a..z} [2]={0..9} [3]=+ [4]=/ [5]== );

Notice that: declare -a statement to an array declaration may speed up execution of subsequent operations on the array:

declare -a bigOne=( /dev/* );           
declare -a bigTwo=( ${bigOne[@]} );

Notice that: any variables can have array operations, even if they are not explicitly declared as arrays:


name=aleen
echo ${name[@]};        
echo ${name[*]};        
echo ${name[0]};        
echo ${name[1]};        


echo ${#name[@]};       

Operations

Add/Replace/Delete elements from an array:


array=(zero one two three);


array[4]=four;
echo ${array[@]};       


array[3]=replace_three;
echo ${array[@]};       


unset array[2];
echo ${array[@]};       


unset array;
echo ${array[@]};       

If you want to read an element from an array, you can write like this:


array=(zero one two three);


echo ${array[0]};       


echo ${array: 2};       
echo ${array[1]: 1};    

If you want to read some lengths like the length of the element or of the array:


array=(zero one two three);


echo ${#array[*]};          
echo ${#array[@]};          


echo ${#array};             
echo ${#array[0]};          

Trailing Substring Extraction:


array=(zero one two three);


echo ${array[@]};           
echo ${array[*]};           
echo ${array[@]: 0};        


echo ${array[@]: 1};        


echo ${array[@]: 0:2};      

Substring removals:


array=(zero one two three four five five);


echo ${array[@]#f*r};       


echo ${array[@]##t*e};      


echo ${array[@]%h*e};       


echo ${array[@]%%t*e};      

String replacement:


array=(four fivefive);


echo ${array[@]/iv/XYZ};                    


echo ${array[@]//iv/XYZ};                   


echo ${array[@]/iv/};                       


echo ${array[@]//iv/};                      


echo ${array[@]/#fi/XYZ};                   


echo ${array[@]/%ve/XYZ};                   

Loading files to an array








array=($(awk "{print $1}" "test.txt"));
echo $(array[*]);                           


n=0;
while read a;
do
    array[$n]=$a;
    ((n++));
done

Empty arrays


array0=( zero one two );
echo ${#array0[@]};                         

array1=("");
echo ${#array1[@]};                         

array2=();
echo ${#array2[@]};                         

array3=(  );
echo ${#array3[@]};                         

Array copying


array_before=( zero one two three );


array_deep_copy=${array_before[@]};

echo Before: ${array_before[@]};            
echo Deep_Copy: ${array_deep_copy[@]};      

array_before[4]=four;

echo Before: ${array_before[@]};            
echo Deep_Copy: ${array_deep_copy[@]};      

Array concatenation


array_before=( zero one );
array_after=( two three );
concat=( ${array_before[@]} ${array_after[@]} );
echo ${concat[*]};