shell介绍

什么是shell?

        在计算机科学中,Shell俗称壳(用来区别于核),是指“提供使用者使用界面”的软件(命令解析器)。它类似于DOS下的command和后来的cmd.exe。它接收用户命令,然后调用相应的应用程序。


  • shell是一个命令解释器,提供用户和机器之间的交互

  • 支持特定语法,比如逻辑判断、循环

  • 每个用户都可以有自己特定的shell

  • CentOS7默认shell为bash(Bourne Agin Shell)

  • 还有zsh、ksh等,只是细微的差别不同


命令历史

    在执行命令时候,可以ongoing键盘上额方向键进行查找之前执行过的历史命令。为什么会有记录呢?由于历史命令都存在当前用户的家目录下,/oot/.bash_history 文件中。

history命令 选项-c

[root@centos7 ~]# history    查看当前存了多少条历史命令
[root@centos7 ~]# history -c    清空当前内存中的历史命令,但无法清空配置文件中的
[root@centos7 ~]# cat /root/.bash_history    查看文件内容
[root@centos7 ~]# echo $HISTSIZE            查看系统配置的环境变量
1000        #默认最大只能存一千条

    可以在etc/profile配置文件中找到HISTSIZE变量重新赋值,当修改值后可以用source /etc/profile命令或重新退出终端再登录生效。

[root@centos7 ~]# HISTTIMEFORMAT="%Y/%m/%d %H:%M:%S "     指定变量的格式
[root@centos7 ~]# echo $HISTTIMEFORMAT                只能当前终端生效
%Y/%m/%d %H:%M:%S                          
#如果需要永久生效可以把变量增到profile文件中,跟HISTSIZE放在一起
[root@centos7 ~]# history        可看到增加了时间参数
    2  2017/11/15 22:34:36 history
    3  2017/11/15 22:35:22 cat /root/.bash_history
    4  2017/11/15 22:37:02 echo $HISTSIZE
    5  2017/11/15 22:38:31 vi /etc/profile
    6  2017/11/15 22:43:38 sudo vi /etc/profile
    7  2017/11/15 22:47:41 HISTTIMEFORMAT="%Y/%m/%d %H:%M:%S "
    8  2017/11/15 22:47:51 echo HISTTIMEFORMAT="%Y/%m/%d %H:%M:%S "
    9  2017/11/15 22:48:15 echo HISTTIMEFORMAT
   10  2017/11/15 22:48:22 HISTTIMEFORMAT="%Y/%m/%d %H:%M:%S "
   11  2017/11/15 22:48:25 echo HISTTIMEFORMAT
   12  2017/11/15 22:48:41 echo $HISTTIMEFORMAT
   13  2017/11/15 22:51:10 history

在有些特定情况下需要对执行过的命令进行记录,可以用chattr + a ~/.bash_history 增加a权限,让文件内容只能追加无法删除。但是非正常退出,命令将保存不全。

[root@centos7 ~]# !!            执行最后一条命令
[root@centos7 ~]# !10           执行第10行历史命令
[root@centos7 ~]# !echo         执行最近一次echo开头的命令

命令补全和别名

        tab键,当输入的匹配的命令只有一个符合的时候,敲一下补全;当输入的匹配命令多个的时候,敲两下显示匹配的结果。

        centos7默认是不支持参数补全的,需要安装yum install -y bash-completion包,安装之后不能使用,需要重启系统才生效。

[root@centos7 ~]# alias restartnet='systemctl restart network.service'  设置别名
[root@centos7 ~]# alias
alias cp='cp -i'
alias egrep='egrep --color=auto'
alias fgrep='fgrep --color=auto'
alias grep='grep --color=auto'
alias l.='ls -d .* --color=auto'
alias ll='ls -l --color=auto'
alias ls='ls --color=auto'
alias mv='mv -i'
alias restartnet='systemctl restart network.service'    #成功设置
alias rm='rm -i'
alias which='alias | /usr/bin/which --tty-only --read-alias --show-dot --show-tilde'
[root@centos7 ~]# unalias restartnet    取消别名

用户都有自己配置别名的文件 ~/.bashrc

ls /etc/profile.d/


通配符

[root@linux1 a]# touch 11.txt 1.txt 2.txt 3.txt  创建文件
[root@linux1 a]# ls
11.txt  1.txt  2.txt  3.txt
# *表示所有 
[root@linux1 a]# ls *.txt
11.txt  1.txt  2.txt  3.txt
#?表示一个任意的字符 
[root@linux1 a]# ls ?.txt
1.txt  2.txt  3.txt
#[0-9]表示0-9范围取一个值 或 [123]表示取1 2 3文件
[root@linux1 a]# ls [1-2].txt
1.txt  2.txt
[root@linux1 a]# ls [12].txt
1.txt  2.txt
#{1,2}表示取1 2文件同等于 [12]
[root@linux1 a]# ls {1,2}.txt
1.txt  2.txt


输入输出重定向

#> >> 分别表示正确 输出重定向 追加输出重定向
[root@linux1 a]# echo "123" > 1.txt
[root@linux1 a]# cat 1.txt
123
[root@linux1 a]# echo "123" >> 1.txt
[root@linux1 a]# cat 1.txt
123
123
#2> 2>> 分别表示错误 输出重定向 追加输出重定向
[root@linux1 a]# cat 1111.txt 2> 2.txt
[root@linux1 a]# cat 2.txt
cat: 1111.txt: 没有那个文件或目录
[root@linux1 a]# cat 1111.txt 2>> 2.txt
[root@linux1 a]# cat 2.txt
cat: 1111.txt: 没有那个文件或目录
cat: 1111.txt: 没有那个文件或目录
#< 表示输入重定向 基本用不到
[root@linux1 a]# wc -l < 1.txt
2
#正确和错误输出重定向
[root@linux1 a]# ls [12].txt aaa.txt &> 3.txt
[root@linux1 a]# cat 3.txt
ls: 无法访问aaa.txt: 没有那个文件或目录
1.txt
2.txt
#分别正确输出重定向3.txt文件 和 错误输出重定向11.txt文件
[root@linux1 a]# ls [12].txt aaa.txt > 3.txt 2> 11.txt
[root@linux1 a]# cat 3.txt
1.txt
2.txt
[root@linux1 a]# cat 11.txt
ls: 无法访问aaa.txt: 没有那个文件或目录