11. Detailed explanation of the declare command and usage examples of 5 types

content

1.declare command and options

2. Example: Numerical operations 

3. Example: Array variable type 

4. Example: Environment Variables 

5. Example: read-only property

6. Example: Query variable properties and cancel variable properties


If we need to do numerical operations, we can do it in any of three ways:

Use declare to declare the variable type: Since the default type of all variables is string type, as long as we declare the variable as integer type, we can perform operations. Declaring the type of a variable is done using the declare command. The command is as follows:

1.declare command and options

【+/-】【sub option】 Variable name

Suboptions:

-: Set the type attribute to the variable.

+: Cancels the type attribute of the variable.

-a: Declare the variable as an array.

-i: Declare the variable as an integer.

-r: Declare the variable as read-only. Note that once a variable is set to read-only, the value of the variable can neither be modified nor deleted, and the read-only attribute cannot even be canceled by +r.

-x: Declare the variable as an environment variable.

-p: Display the declared type of the specified variable.

2. Example: Numerical operations 

[root@localhost ~]# aa=11 
[root@localhost ~]# bb=22 
[root@localhost ~]# declare -i cc=$aa+$bb     #将变量声明为整数型。 
[root@localhost ~]# echo $cc 
33

3. Example: Array variable type 

The so-called array is a collection of elements of the same data type arranged in a certain order, that is, a limited number of variables of the same type are named with a name, and then they are separated by numbers. The name becomes the array and the number is called the subscript. The variables that make up the array are called the components of the array, also called the elements of the array, and are sometimes called the table variables.

Both variables and arrays are used to store data, but a variable can only be assigned one data value. Once assigned repeatedly, the latter value will overwrite the previous value. Arrays, on the other hand, can be assigned a set of data values ​​of the same type.

example:

[root@localhost ~]# name[0]="aa"     #数组中第一个变量是“aa” 
[root@localhost ~]# name[1]="bb"     #数组中第二个变量是“bb”
[root@localhost ~]# name[2]="cc"     #数组中第三个变量是“cc” 
[root@localhost ~]# echo $name       #输出数组的内容,如果只写数名,aa那么只会输出第一个下标变量。
[root@localhost ~]# echo ${name[1]}  #下表1输出了bb 
bb
[root@localhost ~]# echo ${name[*]}  #用数组【*】输出数组所有内容
aa bb cc 
#(数组下表是从0开始的,在调用数组时,需要用${数组[下标]}的方式来读取) 

4. Example: Environment Variables 

We can use the declare command to declare variables as environment variables, which is the same as the export command.

[root@localhost ~]# declare -x test=123     #用declare命令-x选项将test=123声明为环境变量。 
[root@localhost ~]# env | grep test         #在env中查找是否有test的环境变量。
test=123

Environment variables can be used in the shell in which the variable was created and in any subshell or process derived from that shell, so environment variables are often referred to as global variables. The shell in which the environment variable is created is called the parent shell, and if a shell is created in the parent shell, the shell is called a subshell. When a subshell is created, it inherits the environment variables of the parent shell for its own use, so environment variables can be passed from the parent shell to the subshell. Note, however, that environment variables can only be passed down and not up. "Passing on from the son, not the father"

5. Example: read-only property

[root@localhost ~]# declare -r test                  #给test赋予只读属性 
[root@localhost ~]# test=456                         #test变量的值不能修改了 
bash: test: readonly variable 
#bash, 
test,只读变量
[root@localhost ~]# declare +r test                  #试图取消只读属性 
bash: declare: test: readonly variable               #失败.. 
[root@localhost ~]# unset test                       #删除变量
bash: unset: test: cannot unset: readonly variable   #再次失败..... 

Once the read-only attribute is set to a variable, the variable can neither modify the value of the variable nor delete the variable, and the +r option cannot be used to cancel the read-only attribute. However, because this variable is only declared on the command line, it will disappear as soon as you log in again or reboot.

6. Example: Query variable properties and cancel variable properties

The query of variable properties uses the -p option, and the cancellation of variable properties uses the + option. The command is as follows:

[root@localhost ~]# abc=123             #变量赋值abc=123 
[root@localhost ~]# declare -p abc      #用declare -p查看 
declare -- abc="123" 
[root@localhost ~]# declare -i abc      #declare -i声明变量abc为整数型 
[root@localhost ~]# declare -p abc      #查看 
declare -i abc="123" 
[root@localhost ~]# name[0]=aa          #数组 
[root@localhost ~]# name[1]=bb 
[root@localhost ~]# name[2]=cc 
[root@localhost ~]# declare -p name     #查看数组型 
declare -a name='([0]="aa" [1]="bb" [2]="cc")' 
[root@localhost ~]# declare -p test     #test变量是环境变量和只读变量 
declare -rx test="123" 
[root@localhost ~]# declare +x test     #取消test变量的环境变量属性
[root@localhost ~]# declare -p test     #查看test 
declare -r test="123" 

おすすめ

転載: blog.csdn.net/weixin_46659843/article/details/123728542