1 bash shell 介绍
# 介绍
shell是一门解释型、弱类型、动态语言
# 类比
shell语法 <===> python语法
bash解释器 <===> python解释器
平台 <===> 平台
2 变量
2.1 基本使用
### 交互式环境中
[root@localhost ~]# x=1 # 1 定义变量 中间不能有空格
[root@localhost ~]# x=2
[root@localhost ~]# echo $x # 2 使用变量方式一:'$变量'
[root@localhost ~]# name1=egon
[root@localhost ~]# echo $name1
[root@localhost ~]# echo ${name1} # 3 使用变量方式二:'${变量}'
[root@localhost ~]# echo "hello $name"
hello egon
2.2 引号对变量的影响
### 1 双引号 ---> 弱引用 保留变量
[root@localhost ~]# name=egon
[root@localhost ~]# echo "hello $name"
hello egon
### 1 单引号 ---> 强引用 都视为普通字符
[root@localhost ~]# name=egon
[root@localhost ~]# echo 'hello $name'
hello $name
### 1 反引号 ---> 取结果
[root@localhost ~]# today=`date +%F`
[root@localhost ~]# echo $today
2020-08-11
# $():也可以取命令的结果 与反引号的区别是 可以用于嵌套使用
[root@localhost ~]# today=$(date +%H:%M:%S)
[root@localhost ~]# echo $today
22:05:55
# eg:
[root@localhost ~]# tar czf `date +%F`_bak.tar.gz /tmp
# 强调:变量值包含空格时,需要加上双引号包含
[root@localhost ~]# msg="hello egon"
[root@localhost ~]# echo $msg
hello egon
2.3 变量作用域
### 1 变量作用域
环境变量 : 在当前shell及子shell生效!
自定义变量 : 仅在当前shell生效!
### 2 查看变量
set # 查看所有变量(包括自定变量和环境变量)
env # 查看环境变量
### 3 系统环境变量配置文件
1. /etc/profile
2. /etc/bashrc
3. ~/.bashrc
4. ~/.bash_profile
### 4 常见系统环境变量
[root@localhost ~]# echo $PS1 # 命令提示符
[\u@\h \W]\$
[root@localhost ~]# echo $HOSTNAME
localhost.localdomain
[root@localhost ~]# echo $USER
root
[root@localhost ~]# echo $UID
0
[root@localhost ~]# echo $SHELL
/bin/bash
[root@localhost ~]# echo $HISTSIZE # 执行命令 历史记录的保存数量
5
[root@localhost ~]# echo $MAIL
/var/spool/mail/root
[root@localhost ~]# echo $PATH
/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/bin
3 元字符
3.1 常见的特殊字符
# 1 ~ 家目录
# 2 .与..
. 表示当前路径
.. 表示父级路径
# 3 ! 取反
# 4 @ 无特殊意义
# 5 # 注释
# 6 $ 取变量值