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"
。
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 震惊!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中实现列表的倒序排列