Linux 中declare命令

 

Linux 中declare命令

 

001、普通测试

复制代码
[root@PC1 dir1]# ls
[root@PC1 dir1]# echo $var1

[root@PC1 dir1]# var1="hello world"
[root@PC1 dir1]# echo $var1
hello world
[root@PC1 dir1]# var1=100.55
[root@PC1 dir1]# echo $var1
100.55
[root@PC1 dir1]# var1=100
[root@PC1 dir1]# echo $var1
100
复制代码

 

002、声明变量类型为int型

复制代码
[root@PC1 dir1]# ls
[root@PC1 dir1]# echo $var1

[root@PC1 dir1]# declare -i var1                 ## 声明变量为整型
[root@PC1 dir1]# var1="hello world"              ## 定义字符串,报错
-bash: hello world: syntax error in expression (error token is "world")
[root@PC1 dir1]# var1=hello_world                ## 无法输出正确值
[root@PC1 dir1]# echo $var1
0
[root@PC1 dir1]# var1=100.555                    ## 定义浮点数,报错
-bash: 100.555: syntax error: invalid arithmetic operator (error token is ".555")
[root@PC1 dir1]# var1=100                        ## 定义整形,可以正常显示
[root@PC1 dir1]# echo $var1
100
复制代码

 。

 

003、 设置变量为只读

a、

复制代码
[root@PC1 dir1]# ls
[root@PC1 dir1]# echo $var1

[root@PC1 dir1]# declare -r var1          ## 定义变量为只读
[root@PC1 dir1]# var1=100
-bash: var1: readonly variable
[root@PC1 dir1]# var1="hello world"
-bash: var1: readonly variable
[root@PC1 dir1]# var1=hello_word
-bash: var1: readonly variable
[root@PC1 dir1]# declare -r var1="xxxx"             ## 均无法赋值
-bash: declare: var1: readonly variable
复制代码

 

b、

 

c、

复制代码
[root@localhost test]# ls
[root@localhost test]# declare -r var1="hello world"           ## 定义只读变量
[root@localhost test]# echo $var1
hello world
[root@localhost test]# var1=100                                ## 无法给var1变量赋值
-bash: var1: readonly variable
[root@localhost test]# var1="xxx"
-bash: var1: readonly variable
[root@localhost test]# echo $var1
hello world
复制代码

 .

 

004、declare -x选项:指定的变量会成为环境变量,可供shell以外的程序来使用。

复制代码
[root@PC1 test2]# var1=100
[root@PC1 test2]# echo $var1
100
[root@PC1 test2]# export -p | grep "var1"
[root@PC1 test2]# declare -x var1=200
[root@PC1 test2]# echo $var1
200
[root@PC1 test2]# export -p | grep "var1"
declare -x var1="200"
复制代码

 。

 

posted @   小鲨鱼2018  阅读(8)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 震惊!C++程序真的从main开始吗?99%的程序员都答错了
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
· 单元测试从入门到精通
· 上周热点回顾(3.3-3.9)
· winform 绘制太阳,地球,月球 运作规律
历史上的今天:
2024-02-22 slurm中显示所有的节点、或单个节点信息
2024-02-22 slurm系统中 sacct 命令
2023-02-22 linux 中grep命令如何匹配制表符
2023-02-22 linux 中如何从文本中区分空格和tab键
2023-02-22 linux 中 awk命令实现将fasta文件中每个scaffold中的所有碱基转换为一行
2021-02-22 python中实现列表的倒序排列
点击右上角即可分享
微信分享提示