变量赋值方式
1.显示赋值(变量名=变量值)
2.read从键盘读入变量值
3.注意事项:定义或引用变量时注意事项: " " 弱引用 ’ ’ 强引用
所见及所得,输入什么就输出什么,使用单引号
变量,字符串解析使用双引号
4.` `命令替换等价于 $()反引号中的shell命令会先被执行
命令解析执行需要用$()或` `
显示赋值
[root@xiudaochengxian test]# vim fz.sh
1 ip1=192.168.1.105
2 school="Wuhan JiaoTong"
3 today1=`date +%F`
4 today2=$(date +%F)
5
6 echo $ip1
7 echo $school
8 echo $today1
9 echo $today2
[root@xiudaochengxian test]# sh fz.sh
192.168.1.105
Wuhan JiaoTong
2021-08-07
2021-08-07
read从键盘读入变量值
[root@xiudaochengxian test]# vim ping.sh
1 #!/bin/bash
2 read -p "Input IP:" ip
3 ping -c2 $ip &> /dev/null
4 if [ $? -eq 0 ]
5 then
6 echo "host $ip is ok"
7 else
8 echo "host $ip is fail"
9 fi
[root@xiudaochengxian test]# sh ping.sh
Input IP:192.168.1.105
host 192.168.1.105 is ok
定义或引用变量时注意事项: " " 弱引用 ’ ’ 强引用
[root@xiudaochengxian test]# school=100phone
[root@xiudaochengxian test]# echo "${school} is good"
100phone is good
[root@xiudaochengxian test]# echo '${school} is good'
${school} is good
` `命令替换等价于 $()反引号中的shell命令会先被执行
[root@xiudaochengxian test]# touch `date +%F`_file1.txt
[root@xiudaochengxian test]# ls
2021-08-07_file1.txt fz.sh ping.sh
[root@xiudaochengxian test]# touch $(date +%F)_file2.txt
[root@xiudaochengxian test]# ls
2021-08-07_file1.txt 2021-08-07_file2.txt fz.sh ping.sh
[root@xiudaochengxian test]# disk_free3="df -h | grep '/$' |awk '{print $4}'"
[root@xiudaochengxian test]# echo $disk_free3
df -h | grep '/$' |awk '{print }'
[root@xiudaochengxian test]# disk_free4=$(df -h | grep '/$' |awk '{print $4}')
[root@xiudaochengxian test]# echo $disk_free4
14G
[root@xiudaochengxian test]# disk_free5=`df -h | grep '/$' |awk '{print $4}'`
[root@xiudaochengxian test]# echo $disk_free5
14G