Shell Scripting Tutorial [2] - Shell Array

Shell Scripting Tutorial [2] - Shell Array


Directory : https://blog.csdn.net/shn111/article/details/131590488

Reference tutorial : https://www.runoob.com/linux/linux-shell.html

Online editor : https://www.runoob.com/try/runcode.php?filename=helloworld&type=bash


bash supports one-dimensional arrays (does not support multi-dimensional arrays), and does not limit the size of the array.
Similar to C language, the subscripts of array elements start from 0. To obtain the elements in the array, use the subscript, which can be an integer or an arithmetic expression, and its value should be greater than or equal to 0.


create array

Shell arrays are represented by brackets, and elements are separated by "space" symbols. The syntax format is as follows:
array_name=(value1 value2 … valuen)

my_array=(A B "C" D)

Arrays can also be defined using numeric subscripts

array_name[0]=value0
array_name[1]=value1
array_name[2]=value2

read array

The general format for reading the value of an array element is:
${array_name[index]}

my_array=(A B "C" D)

echo "第一个元素为: ${my_array[0]}"
echo "第二个元素为: ${my_array[1]}"
echo "第三个元素为: ${my_array[2]}"
echo "第四个元素为: ${my_array[3]}"

associative array (dictionary)

Associative arrays can use arbitrary strings or integers as subscripts to access array elements

Associative arrays are declaredeclared using commands, and the syntax format is as follows:
declare -A array_name

-A: used to declare an associative array

Associative array keys are unique

Example:

declare -A site=(["google"]="www.google.com" ["runoob"]="www.runoob.com" ["taobao"]="www.taobao.com")

You can also declare an associative array first (you can try without declaring it yourself (if you find that there is still a problem if you don’t declare it, when you get all the elements of the array later, if you don’t declare it, you will only get the last element)), then set the key and value:

declare -A site
site["google"]="www.google.com"
site["runoob"]="www.runoob.com"
site["taobao"]="www.taobao.com"

Use the specified key when accessing the elements of the associative array, the format is as follows:
array_name["index"]

declare -A site
site["google"]="www.google.com"
site["runoob"]="www.runoob.com"
site["taobao"]="www.taobao.com"

echo ${site["runoob"]}
# www.runoob.com

Get all elements in the array

Use @or *can get all elements in the array

Example:

my_array[0]=A
my_array[1]=B
my_array[2]=C
my_array[3]=D

echo "数组的元素为: ${my_array[*]}"
echo "数组的元素为: ${my_array[@]}"
# 数组的元素为: A B C D
# 数组的元素为: A B C D

Associative array:

declare -A site
site["google"]="www.google.com"
site["runoob"]="www.runoob.com"
site["taobao"]="www.taobao.com"

echo "数组的元素为: ${site[*]}"
echo "数组的元素为: ${site[@]}"
# 数组的元素为: www.google.com www.taobao.com www.runoob.com
# 数组的元素为: www.google.com www.taobao.com www.runoob.com

Put an exclamation point in front of the array !to get all the keys of the array

declare -A site
site["google"]="www.google.com"
site["runoob"]="www.runoob.com"
site["taobao"]="www.taobao.com"

echo "数组的键为: ${
     
     !site[*]}"
echo "数组的键为: ${
     
     !site[@]}"
# 数组的键为: google taobao runoob
# 数组的键为: google taobao runoob

Get the length of the array

#The method of obtaining the length of the array is the same as the method of obtaining the length of the string, and the method of obtaining the length of the array is added before the array name

#my_array[*]: Get the number of array elements
#my_array[@]: Get the number of array elements
#my_array[i]: Get the length of the i-th element in the array

my_array[0]=A
my_array[1]=B
my_array[2]=C
my_array[3]=D

echo "数组元素个数为: ${
     
     #my_array[*]}"
echo "数组元素个数为: ${
     
     #my_array[@]}"
echo "数组第0个元素的长度为: ${
     
     #my_array[0]}"

# 数组元素个数为: 4
# 数组元素个数为: 4
# 数组第0个元素的长度为: 1

Guess you like

Origin blog.csdn.net/shn111/article/details/131590604