linux 中declare命令的用法

 

linux 中declare命令的用法

 

001、声明整数型变量

[root@pc1 test1]# declare -i var1       ## 声明变量为整数型变量
[root@pc1 test1]# var1=132              ## 赋值整数型变量
[root@pc1 test1]# echo $var1            ## 输出整数型
132
[root@pc1 test1]# var1=abcd             ## 定义字符串
[root@pc1 test1]# echo $var1            ## 输出为空,说明赋值失败
0
[root@pc1 test1]# var1=8.343            ## 赋值为浮点型,报错
-bash: 8.343: syntax error: invalid arithmetic operator (error token is ".343")

 

002、取消变量属性

[root@pc1 test1]# declare -i var1       ## 定义var1为整数型变量
[root@pc1 test1]# var1=200
[root@pc1 test1]# echo $var1
200
[root@pc1 test1]# var1=xyz
[root@pc1 test1]# echo $var1            ## 无法输出字符串
0
[root@pc1 test1]# var1=3.23             ## 无法赋值浮点
-bash: 3.23: syntax error: invalid arithmetic operator (error token is ".23")
[root@pc1 test1]# declare +i var1       ## 取消变量属性
[root@pc1 test1]# var1=xyz              
[root@pc1 test1]# echo $var1            ## 输出字符串
xyz
[root@pc1 test1]# var1=3.23
[root@pc1 test1]# echo $var1             ## 输出浮点
3.23

 。

 

003、

[root@pc1 test1]# declare -r ab=100
[root@pc1 test1]# echo $ab
100
[root@pc1 test1]# ab=200
-bash: ab: readonly variable
[root@pc1 test1]# ab=xyz
-bash: ab: readonly variable

 。

 

posted @ 2024-03-04 19:53  小鲨鱼2018  阅读(30)  评论(0编辑  收藏  举报