Operation of array in Bash

Time:2021-4-7

preface

Bash only supports one-dimensional arrays, but there is no limit on the number of parameters.

To declare an array:


declare -a array

(in fact, you don’t need to declare it. You can assign a value to a variable in the form of an array. Bash knows that it’s an array.)

1、 Define array

1. Use the operator


names[0]='zrong'
names[1]='jacky'

2. Use () to assign value directly

names=('zrong' 'jacky')
#Or
names=([0]='zrong' [1]='jacky')

3. Usedeclare -aDefines an array. This method can define an empty variable as an array type.


declare -a names

4. Read array from file

cat>names.txt
zrong
jacky
sweet
Ctrl+C
#Read each row as an element of the array
names=(`cat 'names.txt'`)

2、 Read array

1. Array value

Like ActionScript, bash uses the [] operator and a 0-based subscript to take values:

adobe=('Flash' 'Flex' 'Photoshop')
echo ${adobe[0]}
#Printing
# Flash

2. Array length (number of elements)

With the special subscript “@”, you can expand the array into a list, and then you can use the operator “#” in Bash to get the number of elements in the array

adobe=('Flash' 'Flex' 'Photoshop')
echo ${#adobe[@]}
#Printing
# 3

Interestingly, an undefined array subscript does not occupy the number of elements in the array

adobe=([0]='Flash' [2]='Flex' [4]='Photoshop')
echo ${#adobe[@]}
#Printing
# 3

3. Get part of the array

Command substitution is also effective for arrays. You can use the offset operator to get part of the array

adobe=('Flash' 'Flex' 'Photoshop' 'Dreamweaver' 'Premiere')
echo ${adobe[@]:1:3}
#Printing
# Flex Photoshop Dreamweaver
echo ${adobe[@]:3}
#Printing
# Dreamweaver Premiere

4. Connect two arrays

adobe=('Flash' 'Flex' 'Photoshop' 'Dreamweaver' 'Premiere')
adobe2=('Fireworks' 'Illustrator')
adobe3=(${adobe[@]} ${adobe2[@]})
echo ${#adobe3[@]}
#Printing
# 7

3、 Modify array

1. Replace array elements

The pattern operator is also valid for arrays and can be used to replace elements in the array

adobe=('Flash' 'Flex' 'Photoshop' 'Dreamweaver' 'Premiere')
echo ${adobe[@]/Flash/FlashCS5}
#Printing
#Note that the result of printing is a list of strings, not an array
# FlashCS5 Flex Photoshop Dreamweaver Premiere
#
#Resave the replaced value as an array
adobe=(${adobe[@]/Flash/FlashCS5})

2. Delete array elements

Delete array elements by command replacement and reassignment

#Delete Photoshop Elements
adobe=('Flash' 'Flex' 'Photoshop' 'Dreamweaver' 'Premiere')
adobe=(${adobe[@]:0:2} ${adobe[@]:3})
echo ${adobe[@]}
#Printing
# Flash Flex Dreamweaver Premiere

Deleting array elements using pattern operators

adobe=('Flash' 'Flex' 'Photoshop' 'Dreamweaver' 'Premiere')
adobe=(${adobe[@]/Photoshop/})
echo ${adobe[@]}
#Printing
# Flash Flex Dreamweaver Premiere

4、 Cycle

Use the for in loop to read the array:

adobe=('Flash' 'Flex' 'Photoshop' 'Dreamweaver' 'Premiere')
for item in ${adobe[@]};do
 echo $item
done
#Printing
# Flash 
# Flex 
# Photoshop 
# Dreamweaver 
# Premiere

Use the for loop to read the array:

adobe=('Flash' 'Flex' 'Photoshop' 'Dreamweaver' 'Premiere')
len=${#adobe[@]}
for ((i=0;i<$len;i++));do
 echo ${adobe[$i]}
done
#Printing
# Flash 
# Flex 
# Photoshop 
# Dreamweaver 
# Premiere

summary

The above is all about bash array operation, I hope the content of this article can bring some help to your study or work, if you have any questions, you can leave a message to exchange.