shell介绍
shell是一个命令解释器,提供用户和机器之间的交互
支持特定语法,比如逻辑判断、循环
每个用户都可以有自己特定的shell
CentOS7默认shell为bash(Bourne Agin Shell)
还有zsh、ksh等
命令历史
history命令
-d 行号 删除该行
[root@localhost ~]# vi /root/.bash_history //history命令存放文件
.bash_history
[root@localhost ~]# echo $HISTSIZE //变量HISTSIZE最大1000条
1000
[root@localhost ~]# history -c //清空内存命令历史
[root@localhost ~]# history -w //保存文件到文件里去
[root@localhost ~]# vi /etc/profile // /etc/profile中修改HISTSIZE
[root@localhost ~]# source /etc/profile
HISTTIMEFORMAT="%Y/%m/%d %H:%M:%S "
[root@localhost ~]# HISTTIMEFORMAT="%Y/%m/%d %H:%M:%S "
[root@localhost ~]# echo $HISTTIMEFORMAT
%Y/%m/%d %H:%M:%S
[root@localhost ~]# history
62 2017/10/26 21:34:47 history
63 2017/10/26 21:34:57 vi /etc/profile
64 2017/10/26 21:36:31 echo $HISTSIZE
[root@localhost ~]# chattr +a /root/.bash_history
永久保存 chattr +a ~/.bash_history
!! 执行历史命令最后一个命令
!n 执行历史命令的某个命令(n数字)
!word历史命令从下往上找以word开头的第一个命令
命令补全和别名
tab键
敲一下:命令补全
敲两下:列出以某些字符开头的命令
参数补全,安装bash-completion
[root@localhost ~]# yum install -y bash-completion
[root@localhost ~]# reboot //重启
[root@localhost ~]# rpm -q bash-completion
bash-completion-2.1-6.el7.noarch
alias别名给命令重新起个名字
[root@localhost ~]# alias resrenet='systemctl restart network'
各用户都有自己配置别名的文件 ~/.bashrc
[root@localhost ~]# vi .bashrc
其他的别名所在
ls /etc/profile.d/
[root@localhost ~]# ls /etc/profile.d/
自定义的alias放到~/.bashrc
通配符
ls *.txt
[root@localhost profile.d]# ls *.sh //*通配.sh结尾的脚本文件
256term.sh colorgrep.sh lang.sh vim.sh
bash_completion.sh colorls.sh less.sh which2.sh
[root@localhost profile.d]# ls *.cs* //不分字符、不分几个
256term.csh colorls.csh less.csh which2.csh
ls ?.txt
[root@localhost profile.d]# ls *.?sh //一个任意的字符
256term.csh colorls.csh less.csh which2.csh
ls [0-9].txt
[root@localhost ~]# ls [0-2].txt //0-2任意一个都满足
1.txt 2.txt
ls {1,2}.txt
[root@localhost ~]# ls {0,1,2}.txt //大括号里的必须要有,没有会报错。类似[0-2]但是要,隔开
ls: 无法访问0.txt: 没有那个文件或目录
1. txt 2.txt
输入输出重定向
cat 1.txt >2.txt
[root@localhost ~]# echo "22">2.txt
[root@localhost ~]# echo "11">1.txt
[root@localhost ~]# cat 1.txt > 2.txt 重定向覆盖
[root@localhost ~]# cat 2.txt
11
cat 1.txt >> 2.txt
[root@localhost ~]# cat 2.txt
11
[root@localhost ~]# echo "22">1.txt
[root@localhost ~]# cat 1.txt >> 2.txt //追加重定向
[root@localhost ~]# cat 2.txt
11
22
ls aaa.txt 2>err
[root@localhost ~]# ls aaa.txt 2>1.txt //2错误重定向
[root@localhost ~]# cat 1.txt
ls aaa.txt 2>>err
[root@localhost ~]# cat 1.txt
ls: 无法访问aaa.txt: 没有那个文件或目录
[root@localhost ~]# ls aaa.txt 2>>1.txt //错误追加重定向
[root@localhost ~]# cat 1.txt
ls: 无法访问aaa.txt: 没有那个文件或目录
ls: 无法访问aaa.txt: 没有那个文件或目录
[root@localhost ~]# > >> 2> 2>> >+2>=== &>
[root@localhost ~]# ls {1,3,2,0}.txt &> >lsx.txt //正确输出和错误输出都到lsx.txt里去
[root@localhost ~]# cat lsx.txt
ls: 无法访问3.txt: 没有那个文件或目录
ls: 无法访问0.txt: 没有那个文件或目录
1.txt
2.txt
wc -l < 1.txt
[root@localhost ~]# cat 1.txt
ls: 无法访问aaa.txt: 没有那个文件或目录
ls: 无法访问aaa.txt: 没有那个文件或目录
[root@localhost ~]# wc -l < 1.txt //输入重定向
2
command >1.txt 2>&1
[root@localhost ~]# ls {1,3,2,0}.txt >> lsx.txt 2>>lshx.txt //正确的输入到lsx.txt 错误的输入到lshx.txt
[root@localhost ~]# cat lshx.txt
ls: 无法访问3.txt: 没有那个文件或目录
ls: 无法访问0.txt: 没有那个文件或目录