1.通配符
1.1 作用于文件
*
:匹配任意0个或多个字符或字符串,包括空字符串[root@VM-0-17-centos ~]# rm -f *.sh
?
:匹配任意1个字符[root@VM-0-17-centos ~]# ls ?.* a.out [root@VM-0-17-centos ~]# ls ????.* # 可以多次使用 info.txt test.sh
1.2 作用于文件/字符
[abcd]
:匹配括号中abcd
任意1个字符[root@VM-0-17-centos ~]# ls [abcd].* a.out
[a-z]
:匹配括号中a-z
中任意1个字符[root@VM-0-17-centos ~]# ls [a-c].* a.out
[!b-z]|[\^b-z]
:不匹配括号中b-z
中任意1个字符[root@VM-0-17-centos ~]# ls [!b-d].* a.out [root@VM-0-17-centos ~]# ls [^b-d].* a.out
tips:
- 使用了
[a-z]
必须要保证其中是连续的,比如[c-a]
会报错
2.特殊字符
2.1 与路径和位置相关
~
:用户家目录[root@VM-0-17-centos ~]# cd ~ [root@VM-0-17-centos ~]# pwd /root
-
:上一次用户所在路径[root@VM-0-17-centos etc]# cd - /root [root@VM-0-17-centos ~]# # 显示完会跳转到上一次用户所在路径
.
:当前目录..
:上一级目录tips:
echo $OLDPWD
可以返回上一次用户所在路径,所以cd -
实际就是cd $OLDPWD
2.2 引号*
- 单引号’ ’ :强引用,输出时会把引号中的内容原样输出,不会执行其中的命令等
[root@VM-0-17-centos ~]# echo '`date`' `date`
- 双引号" ":弱引用,如果引号的内容中有命令(需要反引号)、变量、转义字符等,会先把这些内容进行解析再输出
[root@VM-0-17-centos ~]# echo "`date`" Sat Feb 19 16:42:06 CST 2022
- 反引号``:引用命令,相当于$()
- 无引号:遇到空格时会将整体切分,最好使用双引号
[root@VM-0-17-centos ~]# a=psj ps2 -bash: ps2: command not found
2.3 其他特殊字符
;
:表示一个命令的结束,也是命令间的分隔符[root@VM-0-17-centos ~]# echo a;cat test.sh a # echo a #!/bin/bash # cat test.sh
#
:注释内容,同时也是root用户的命令提示符,在vim
中作为分隔符::%s#i#j#g # 将文本中所有的i替换为j
$
:表示字符串变量内容,同时也是普通用户的命令提示符\
:逃脱符/转义字符,将特殊含义的字符进行还原,同时也作为换行符|
:管道{}
:生成序列,也可以作为引用变量和普通字符间的分隔[root@VM-0-17-centos ~]# echo {1..5} # 相当于seq 5 1 2 3 4 5 [root@VM-0-17-centos ~]# echo ${a}2 psj2 [root@VM-0-17-centos ~]# echo {1..a} # 不存在1到a这样的连续序列,所以当作字符串输出 {1..a}
tips:
- 管道传输的数据流(数据内容),不是文件(名称)
seq 1 1 5
表示起始点 步长 终点
2.4 bash中的特殊字符
&&
:连接两个命令时,前一个命令正确,后一个命令才会执行,如果前一个错误就不再执行后面命令[root@VM-0-17-centos ~]# echo a && cat test.sh a #!/bin/bash [root@VM-0-17-centos ~]# cho a && cat test.sh -bash: cho: command not found
||
:连接两个命令时,前一个命令错误,后一个命令才会执行,如果前一个正确就不再执行后面命令[root@VM-0-17-centos ~]# echo a || cat test.sh a [root@VM-0-17-centos ~]# cho a || cat test.sh -bash: cho: command not found #!/bin/bash
!
:在bash中表示取反;在vim中表示强制;直接在命令行中使用表示找到以某字符串开头的命令并运行[root@VM-0-17-centos ~]# !c cho a || cat test.sh # 以c开头的命令行 -bash: cho: command not found # 执行该命令 #!/bin/bash