Shell full analysis (2): string variables and array variables

I. Introduction

This article mainly introduces string variables and array variables in Shell scripts.

Just remember that the array types in Shell are strings and arrays, basically all the work can be done, let's start.

2. Shell String Variables

2.1 Single, double and backticks

There are three kinds of quotation marks in the shell, single quotation mark '', double quotation mark "" and back quotation mark ``.

Let's talk about single quotes '' and double quotes "" first, as follows:

The same point: both solve the problem of spaces in the middle of variables. Because "space" is a very special character in bash, for example, if str=this is String is defined in bash, an error will be reported. In order to avoid errors, single quotation marks '' and double quotation marks "" must be used.

The difference: the difference between single quotation marks '' and double quotation marks "" is that the single quotation mark '' deprives all characters of the special meaning, and the single quotation mark '' becomes a simple character. The exception is for parameter substitution ($) and command substitution (``) within double quotes "". For example enter:

n=3
echo '$n'

but
insert image description here

Look at the backticks again. Backticks are command substitution. Command substitution means that the shell can execute the command in `` first, temporarily save the output result, and output it in an appropriate place. In the following example, the result of the command execution is stored in a variable:

#!/bin/bash

DATE=`date`
echo "Date is $DATE"

UP=`date ; uptime`
echo "Uptime is $UP"

insert image description here

Summary: The core function of backticks is command substitution, wrapping the command to be executed, and wrapping the English semicolon is a newline.
Single quotation marks and double quotation marks are different:
variables can be identified in double quotation marks, but variables cannot be identified in single quotation marks;
escape characters can be identified in double quotation marks, but escape characters cannot be identified in single quotation marks.

2.2 Three forms of strings

In Shell, strings include three forms: no quotation marks, single quotation marks, and double quotation marks, as follows:

your_name="runoob"
echo $your_name

# 这里会被解释为一个被双引号包裹的字符串,加$符号的变量会被读取
str="Hello, I know you are $your_name! \n"
echo $str

# 这里会被解释为两个双引号字符串拼接,即""+变量+"",加$符号的变量会被读取
str="Hello, I know you are "$your_name"! \n"
echo $str

# 引号无法解释变量的
str='Hello, I know you are "$your_name"! \n'
echo $str

# 双引号内部可以加转义字符,转义双引号为普通字符,而不是被解释为双引号字符串拼接
str="Hello, I know you are \"$your_name\"! \n"
echo $str

str="Hello, I know you are '$your_name'! \n"
echo $str

str="Hello, I know you are \'$your_name\'! \n"
echo $str

insert image description here

2.3 String variable assignment and reading

echo $a
echo a

insert image description here

a=b
echo $a

insert image description here

2.4 String concatenation

your_name="runoob"

# 不使用引号,则赋值号右边的字符串中不能包含空格,因为不能包含空格,但是$符号
# 默认是空格确定变量名的,如果变量名后面还是字符串,要读取的变量必须加上{}
greeting_0=hello,$your_name
echo $greeting_0
# $符号默认是空格确定变量名的,如果变量名后面还是字符串,要读取的变量必须加上{}
greeting_0=hello,${your_name}!
echo $greeting_0

# 使用双引号拼接
greeting_1="hello, "$your_name" !"
greeting_2="hello, "${your_name}" !"
greeting_3="hello, $your_name !"
greeting_4="hello, ${your_name} !"
echo $greeting_1
echo $greeting_2
echo $greeting_3
echo $greeting_4


# 使用单引号拼接
greeting_5='hello, '$your_name' !'
greeting_6='hello, '${your_name}' !'
greeting_7='hello, $your_name !'
greeting_8='hello, ${your_name} !'
echo $greeting_5  
echo $greeting_6
echo $greeting_7
echo $greeting_8

insert image description here

2.5 Get string length

Add # in front of the string to get the length of the string without calling the method

string="abcd"
echo ${
    
    #string}   # 输出 4
echo ${
    
    #string[0]}   # 输出 4

insert image description here

2.6 Extracting substrings

The index value of the first character is 0, for example, truncating 4 characters from the second character of the string:

string=“runoob is a great site”
echo ${string:1:4} # 输出 unoo

insert image description here

Three, shell array

Bash only supports one-dimensional arrays, not multi-dimensional arrays. It can not use continuous subscripts, and there is no limit to the size of the array, that is, the array is of indeterminate length and can have unlimited elements in the back. The subscripts of the array elements are numbered starting from 0. To get the elements in the array, use the subscript. The subscript can be an integer or an arithmetic expression, and its value should be greater than or equal to 0.

3.1 Array Definition

In the Shell, parentheses are used to denote arrays, and array elements are separated by "space" symbols. The general form of defining an array is:

数组名=(值1 值2 ... 值n)

E.g:

array_name=(value0 value1 value2 value3)

or

array_name=(
value0
value1
value2
value3
)

It is also possible to define the individual components of the array individually:

array_name[0]=value0
array_name[1]=value1
array_name[n]=valuen

3.2 Array assignment

It's simple, it's

array_name[0]=value0
array_name[1]=value1
array_name[n]=valuen

3.3 Array reading

The general format for reading the value of an array element is:

${
    
    数组名[下标]}

E.g:

valuen=${array_name[n]}

Use the @ symbol to get all the elements in the array, or use * to get all the elements in the array, for example:

echo ${array_name[@]}
echo ${array_name[*]}

3.4 Get array length

The method of obtaining the length of an array is the same as that of obtaining the length of a string, which is to add a # in front of the array variable name, for example:

Example

# 取得数组元素的个数
length=${
    
    #array_name[@]}
# 或者
length=${
    
    #array_name[*]}
# 取得数组单个元素的长度
lengthn=${
    
    #array_name[n]}

Array definition, assignment, reading, and length calculation are as follows:

insert image description here
Summary: In Shell, operations on arrays include:
(1) When it is time to write an array element, just add the assignment number and constant/variable directly behind it.
(2) When reading an array element, it is necessary to wrap a layer on the array element ${}before reading the element. In
particular, to obtain all elements in the entire array, change the subscript number in the square brackets to @OR *, which is ${array_name[@]} ${array_name[*]}generally used in for var in arrayIn the loop, the array here needs to get all the elements of the array.
(3) Get the length of a single array, wrap a layer on top of the array elements ${#}, and get the length of the entire array (the number of elements in the entire array), that is ${#array_name[@]} ${#array_name[*]}, it for ((i=0;i<${#array[*]};i++))is used in .

Fourth, shell comments

The shell has two ways of commenting,
(1) Lines preceded with # will be considered comments and will be ignored by the interpreter.
(2) Use the :<< comment as follows:

:<<EOF
注释内容...
注释内容...
注释内容...
EOF

EOF can also use other symbols:

实例
:<<'
注释内容...
注释内容...
注释内容...
'
:<<!
注释内容...
注释内容...
注释内容...
!

Five, the end

This article mainly introduces string variables and array variables in Shell scripts.

Code every day, progress every day! !

Guess you like

Origin blog.csdn.net/qq_36963950/article/details/123460539