bash及其特性
bash及其特性
shell:外壳
GUI:Gnome,KDE,Xfce
CLI:sh,csh,ksh,bash(Bourne-Again SHell),tcsh,zsh
不同用户启动相同sh程序,但是是不同的进程。
进程:在每个进程看来,当前主机上只存在内核和当前进程。
进程是程序的副本,也是程序执行的实例,具有生命周期。
用户工作环境
bash:不同用户的工作环境设定可以不同。
# root
$user
shell:有子shell的概念。可以交互打开。
bash:
1、命令历史,命令补全
2、管道、重定向
3、命令别名
4、命令行编辑
5、命令行展开
6、文件名通配
7、支持变量
8、支持编程
特性:
一、命令行编辑
光标跳转:Ctrl+a 跳到命令行首
Ctrl+e 跳到命令行尾
Ctrl+u 删除光标至行首的内容
Ctrl+k 删除光标至行尾的内容
Ctrl+左右箭头 一次跳一个单词
Ctrl+l 清屏
二、命令历史
自动记录使用过的命令,保存在内存的一段缓冲区中。
使用 history 查看命令历史。
-c:清空命令历史
-d OFFSET [n]:删除指定位置历史列表。 history -d 300 20
当用户退出,会在用户家目录下追加此前的命令历史到.bash_history隐藏文件。
-w:将缓冲区的命令历史至历史文件中
命令补全:
在PATH环境变量下搜索补全。
路径补全:
环境变量:
PATH:命令搜索路径
HISTSIZE:命令历史大小
[plain] view plain copy
1. [root@localhost ~]# echo $HISTSIZE
2. 1000
命令历史的使用技巧:
!n :执行命令历史中的第n条命令
[plain] view plain copy
1. 324 history
2. 325 ls -a
3. 326 echo $HISTSIZE
4. 327 history
5. [root@localhost ~]# !326
6. echo $HISTSIZE
7. 1000
!-n:执行历史中倒数第n条命令。
!!:执行上一条命令。
[plain] view plain copy
1. 328 echo $HISTSIZE
2. 329 history
3. [root@localhost ~]# !-2
4. echo $HISTSIZE
5. 1000
6. [root@localhost ~]# !!
7. echo $HISTSIZE
8. 1000
9. [root@localhost ~]# !!
10. echo $HISTSIZE
11. 1000
!string: 执行命令历史中最近一个以指定字符串开头的命令。
!$:引用前一个命令的最后一个参数。
Esc, . :按下Esc,再按‘点’,引用最后一个参数。
三、命令别名
aliasCMDALIAS='COMMAND [option] [arguments]'
仅在当前shell进程的生命周期有用。别名的有效范围仅为当前shell进程。
[plain] view plain copy
1. [root@localhost ~]# alias cls=clear
撤销别名
[plain] view plain copy
1. [root@localhost ~]# unalias cls
\CMD : 来使用命令本身,而不是别名(当别名和命令名称一样时)
当在~/.bashrc下编辑别名后,可使用 source .bashrc 命令运行bash。直接使用别名
四、命令替换 $(),或反引号 `COMMAND`
把命令中某个子命令替换为其执行结果的过程。
[plain] view plain copy
1. [root@localhost sysconfig]# echo "The current directory is $(pwd)."
2. The current directory is /etc/sysconfig.
3. [root@localhost sysconfig]# cd /root/
4. [root@localhost ~]# echo "The current directory is $(pwd)."
5. The current directory is /root.
[plain] view plain copy
1. [root@localhost ~]# echo "The current directory is `pwd`."
2. The current directory is /root.
创建 file-2016-5-17-18-52. txt
[plain] view plain copy
1. [root@localhost ~]# touch ./file-$(date +%F-%H-%M-%S).txt
bash支持的引号:
``:反引号,命令替换
"":弱引用,可以实现变量替换,将变量名替换为变量值
'':强引用,不完成变量替换。
五、文件名通配,globing
*:匹配任意长度的任意字符
?:匹配任意单个字符
[]:匹配指定范围内的任意单个字符
[abc],[a-z],[A-Z],[0-9],[a-zA-Z]
[^]:匹配指定范围外的任意单个字符
[plain] view plain copy
1. [root@localhost test]# touch a123 abc ab123 xyz x12 xyz123
2. [root@localhost test]# ls
3. a123 ab123 abc x12 xyz xyz123
4. [root@localhost test]# ls a*
5. a123 ab123 abc
6. [root@localhost test]# ls a*3
7. a123 ab123
8. [root@localhost test]# ls *y*
9. xyz xyz123
10. [root@localhost test]# touch helloy123
11. [root@localhost test]# ls *y*
12. helloy123 xyz xyz123
13. [root@localhost test]# touch y123
14. [root@localhost test]# ls *y*
15. helloy123 xyz xyz123 y123
16. [root@localhost test]# ls ?y*
17. xyz xyz123
18. [root@localhost test]# touch 123
19. [root@localhost test]# ls [a-zA-Z]*[0-9]
20. a123 ab123 helloy123 x12 xyz123 y123
[plain] view plain copy
1. [root@localhost test]# ls [^0-9]*
2. a123 ab123 abc helloy123 x12 xyz xyz123 y123
[:space:]: 所有的空白字符 [[:space:]] 空白字符
[:punct:] :所有 标点符号
[:lower:]: 小写字母
[:upper:]: 大写字母
[:alpha:]:大小写字母
[:digit:]: 数字
[:alnum:] : 数字和大小写字母
[plain] view plain copy
1. [root@localhost test]# man 7 glob //查看
[plain] view plain copy
1. [root@localhost test]# ls [[:alpha:]]*[[:space:]]*[[:alpha:]] //<span style="font-size: 13.3333px;">匹配文件名中有空白字符的文件</span>
2. a b
[plain] view plain copy
1. [root@localhost test]# ls [[:alpha:]]*[^[:alpha:]] //字母开头,非字母结尾
2. a123 ab123 helloy123 x12 xyz123 y123
转载于:https://blog.51cto.com/pengge666/1901244