1. See the script for details
#!/bin/bash# declaring an array declare -a test#j=0#cat > test.file << eof#1#2#3#4#5# eof# writes the value of the file to the array #for line in $ (cat test.file) #do # test[${j}]=${line}# let j+=1#done# defines the contents of an array test[0]=1test[1]=2test[2 ]=3test[3]=4test[4]=5# Print array contents echo ${test[@]} #打印数组所有元素: ${array_name[@]} or ${array_name[*]}# Print array elements way one echo "One way to print all of the array elements" for i in ${test[@]}do echo ${i}done# Printing array elements Mode two echo "Another way to print all of the array elements" for i in ${test[*]}do echo ${i}done# Get Array Length # Law one echo "Get array length" echo "solution 1" echo ${#TEST [*]}echo "solution 2 "#法二echo ${#Test[@]} #打印数组的第四个元素echo "Print the fourth array element" echo ${test[3]}# Delete the fourth element of the array echo "Delete fourth array element" unset test[3]for i in ${ test[@]}do echo ${i}done# delete the entire array and no more print group elements after deletion unset Testfor i in ${test[@]}do echo ${i}done
2. Implementation results
[[Email protected] ~]# sh a.sh 1 2 3 4 5one A-print all of the array elements12345another A-to-print all of the ARR Ay elements12345get array lengthsolution 15solution 25print the fourth array element4delete Fourth array element #此处可以看到没 There is a print out of the deleted array element 41235
Shell Array Common operations