1.环境变量
环境变量也可叫全局变量,可以在创建他们的shell及派生出的子shell中使用(无需定义,直接可以使用,如:$UID)
相关命令:
set :输出所有变量
env:只显示全局变量
declare:输出所有变量,函数,整数等
2.普通变量
1)普通变量赋值
没有特别要求时,字符串都加双引号,需要原样输出就加单引号。
[root@localhost Shell.scripts]# a=123
[root@localhost Shell.scripts]# b=456
[root@localhost Shell.scripts]# echo $a+$b
123+456
[root@localhost Shell.scripts]# echo "$a+$b"
123+456
[root@localhost Shell.scripts]# echo '$a+$b'
$a+$b
2)特殊变量
$0:获取脚本文件名,如果执行时包含路径,则输出脚本路径
$n(>0):
$#:统计传入的参数个数
$?:检测上条命令执行结果的返回值,0表示执行成功,非0表示执行失败
$$:获取当前bash的进程号
示例:
1)$0
[root@localhost Shell.scripts]# vim test.sh
#!/bin/bash
#echo $0
[root@localhost Shell.scripts]# chmod +x test.sh
[root@localhost Shell.scripts]# ./test.sh
./test.sh
[root@localhost Shell.scripts]# /Shell.scripts/test.sh
/Shell.scripts/test.sh
[root@localhost Shell.scripts]# sh test.sh
test.sh
2)$n
[root@localhost Shell.scripts]# vim westos.sh
#!/bin/bash
echo $1 $2 $3 $4 $5 $6 $7 $8 $9 $10
[root@localhost Shell.scripts]# sh westos.sh {a..z}
a b c d e f g h i a0
[root@localhost Shell.scripts]# vim westos.sh
#!/bin/bash
echo $1 $2 $3 $4 $5 $6 $7 $8 $9 ${10}
[root@localhost Shell.scripts]# sh westos.sh {a..z}
a b c d e f g h i j
3)$#
[root@shell Shell.scripts]# vim testtest.sh
#!/bin/bash
echo $1 $2 $3 $4 $5 $6 $7 $8 $9 ${10}
echo $#
[root@shell Shell.scripts]# sh testtest.sh {1..100}
1 2 3 4 5 6 7 8 9 10
100
4)$?
[root@shell Shell.scripts]# cat /etc/password
cat: /etc/password: No such file or directory
[root@shell Shell.scripts]# echo $?
1
5)$$
[root@localhost Shell.scripts]# echo $$
31221
3.从命令行输入
[root@shell Shell.scripts]# read -p "Please input a namber" a
Please input a namber 10
[root@shell Shell.scripts]# echo $a
10
4.将命令的结果赋给变量
1)方法一
[root@shell Shell.scripts]# CMD=`ls -l`
[root@shell Shell.scripts]# echo $CMD
total 52 -rw-r--r--. 1 root root 882 Dec 22 14:31 ifconfig -rw-r--r--. 1 root root 469 Dec 22 13:04 messages-clear.sh -rw-r--r--. 1 root root 84 Dec 22 11:05 messages-tar.sh -rw-r--r--. 1 root root 76 Dec 22 16:06 sort.sh -rw-r--r--. 1 root root 52 Dec 22 15:49 test -rw-r--r--. 1 root root 58 Dec 22 16:38 test1.sh -rw-r--r--. 1 root root 113 Dec 22 17:00 test2.sh -rw-r--r--. 1 root root 273 Dec 22 17:22 test3.sh -rwxr-xr-x. 1 root root 123 Dec 22 15:21 test.sh -rw-r--r--. 1 root root 20 Dec 22 19:39 testtest.sh -rwxr-xr-x. 1 root root 129 Dec 22 13:30 username.sh -rw-r--r--. 1 root root 58 Dec 22 15:01 westos.sh -rw-r--r--. 1 root root 195 Dec 22 11:50 yunsuan.sh
2)方法二
[root@shell Shell.scripts]# DDD=$(ls -l)
total 52 -rw-r--r--. 1 root root 882 Dec 22 14:31 ifconfig -rw-r--r--. 1 root root 469 Dec 22 13:04 messages-clear.sh -rw-r--r--. 1 root root 84 Dec 22 11:05 messages-tar.sh -rw-r--r--. 1 root root 76 Dec 22 16:06 sort.sh -rw-r--r--. 1 root root 52 Dec 22 15:49 test -rw-r--r--. 1 root root 58 Dec 22 16:38 test1.sh -rw-r--r--. 1 root root 113 Dec 22 17:00 test2.sh -rw-r--r--. 1 root root 273 Dec 22 17:22 test3.sh -rwxr-xr-x. 1 root root 123 Dec 22 15:21 test.sh -rw-r--r--. 1 root root 20 Dec 22 19:39 testtest.sh -rwxr-xr-x. 1 root root 129 Dec 22 13:30 username.sh -rw-r--r--. 1 root root 58 Dec 22 15:01 westos.sh -rw-r--r--. 1 root root 195 Dec 22 11:50 yunsuan.sh
5.变量的数值计算
1)方式一
[root@shell Shell.scripts]# a=123
[root@shell Shell.scripts]# expr $a + 10
133
[root@shell Shell.scripts]# expr $a - 10
113
[root@shell Shell.scripts]# expr $a * 10
expr: syntax error
[root@shell Shell.scripts]# expr $a \* 10
1230
[root@shell Shell.scripts]# expr $a / 10
12
[root@shell Shell.scripts]# expr $a % 10
3
2)方式二:$[ ] | $(( ))
[root@shell Shell.scripts]# a=10
[root@shell Shell.scripts]# echo $[a+10]
20
[root@shell Shell.scripts]# echo $[a-10]
0
[root@shell Shell.scripts]# echo $[a*10]
100
[root@shell Shell.scripts]# echo $[a/10]
1
[root@shell Shell.scripts]# echo $[a%10]
0
[root@shell Shell.scripts]# a=10
[root@shell Shell.scripts]# echo $((a+10))
20
[root@shell Shell.scripts]# echo $((a-10))
0
[root@shell Shell.scripts]# echo $((a*10))
100
[root@shell Shell.scripts]# echo $((a/10))
1
[root@shell Shell.scripts]# echo $((a%10))
0
3)方式三let:将计算的值赋给变量
[root@shell Shell.scripts]# let a+=10
[root@shell Shell.scripts]# echo $a
20
[root@shell Shell.scripts]# let a-=10
[root@shell Shell.scripts]# echo $a
10
[root@shell Shell.scripts]# let a*=10
[root@shell Shell.scripts]# echo $a
100
4)方式四:bc常用来计算小数
[root@shell Shell.scripts]# echo 1.2+3.4 | bc
4.6
[root@shell Shell.scripts]# echo "scale=1;1.2*3.4" | bc
4.0