shell学习笔记02-环境变量-0609

1、环境变量
声明变量
export name=VALUE
declare -x name=VALUE
变量引用
$name   ${name}
显示所有环境变量
env  
printenv
export
declare -x
删除变量
uset name

只读变量:只能声明定义,但后续不可修改和删除,即常量
readonly name
declare -r name
范例
[06:40:27 root@localhost ~]#readonly PI=3.1414926
[06:40:47 root@localhost ~]#echo $PI
3.1414926
[06:40:56 root@localhost ~]#PI=3.14
-bash: PI: 只读变量
[06:41:11 root@localhost ~]#unset IP
[06:41:25 root@localhost ~]#echo $PI
3.1414926

查看只读变量
readonly [-p]
declare -r

位置变量:在bash shell中内置的变量,在脚本代码中调用通过命令行传递给脚本的参数
$1,$2,... 对应的第一个,第二个等参数
$0     命令本身,包括路径
$*    传递给脚本所有参数,全部参数合为一个字符串
$@    传递给脚本所有参数,每个参数为独立字符串
$#    传递给脚本的参数个数
注意:$@和$*只有在被双引号包起来的时候才会有差异
set -- :清空所有位置变量

范例
bash /data/arg.sh {a..m}
#!/bin/bash
echo "1st arg is $1"
echo "2st arg is $2"
echo "3st arg is $3"
echo "10st arg is ${10}"
echo "11st arg is ${11}"
echo "The num of arg is $#"
echo "All args are $*"
echo "All args are $@"
echo "The scriptname is `basename $0`"

[06:59:00 root@localhost data]#bash /data/arg.sh {a..m}
1st arg is a
2st arg is b
3st arg is c
10st arg is j
11st arg is k
The num of arg is 13
All args are a b c d e f g h i j k l m
All args are a b c d e f g h i j k l m
The scriptname is arg.sh

 

posted @ 2020-06-12 07:00  duanpeng123  阅读(87)  评论(0)    收藏  举报