Shell是一个命令解释器,提供用户和机器之间的交互,支持特定的语法,好比逻辑判断、循环。每一个用户均可以有本身特定的shell,CentOS7的默认shell为bash(Bourne Agin Shell),常见的还有zsh(power-shell)、ksh(Korn shell)。css
语法: history [-c]
-c:=clear 清除内存中的命令,不能删除配置文件中的历史命令linux
[root@3 ~]# history 1 ls 2 ls /tmp/ 3 ls /boot/ 4 ls / 5 dhclient …… [root@3 ~]# ls /root/.bash_history /root/.bash_history history的家目录
显示使用过的命令历史,默认保存1000条使用过的命令(注:此令须要是在正常关机操做状况下的处1000条命)!shell
[root@3 ~]# echo $HISTSIZE 1000
该变量决定命令历史保存的命令的数目。vim
编辑其配置文件 [root@3 ~]# vim /etc/profile …… HOSTNAME=`/usr/bin/hostname 2>/dev/null` HISTSIZE=1000 …… [root@3 ~]# echo $HISTSIZE 1000 [root@3 ~]# source /etc/profile [root@3 ~]# echo $HISTSIZE 2000
搜索关键字"HIST"找到‘HISTSIZE=1000’,在此更改其数字,保存退出,而后执行命令‘source /etc/profile’刷新该配置文件才会生效。bash
[root@3 ~]# echo $HISTTIMEFORMAT [root@3 ~]# HISTTIMEFORMAT="%Y/%m/%d %H:%M:%S " [root@3 ~]# echo $HISTTIMEFORMAT %Y/%m/%d %H:%M:%S [root@3 ~]# history 1 2017/06/28 18:50:11 history 2 2017/06/28 18:51:32 echo $HISTTIMEFORMAT 3 2017/06/28 18:51:43 HISTTIMEFORMAT="%Y/%m/%d %H:%M:%S " 4 2017/06/28 18:51:45 echo $HISTTIMEFORMAT 5 2017/06/28 18:52:32 history
直接为‘HISTTIMEFORMAT’赋值便可,不过此时该格式只适用于当前终端。若是要其使用于全部用户,则须要将其写入history配置文件并刷新后生效。less
[root@3 ~]# vim /etc/profile …… HOSTNAME=`/usr/bin/hostname 2>/dev/null` HISTSIZE=1000 HISTTIMEFORMAT="%Y/%m/%d %H:%M:%S " …… 保存退出! [root@3 ~]# source /etc/profile
[root@3 ~]# chattr +a ~/.bash_history
使用文件特殊权限,为‘.bash_history’文件配置‘a’权限(只可追加,不可删除),限于正常关机操做。spa
[root@3 ~]# w …… [root@3 ~]# !! w ……
‘!’的用法:‘!n’(n表明数字),表示运行命令历史中的第n条命令;‘!word’,表示运行上一次以该word开头的命令。
eg:code
[root@3 ~]# history 1 2017/06/28 18:50:11 history 2 2017/06/28 18:51:32 echo $HISTTIMEFORMAT 3 2017/06/28 18:51:43 HISTTIMEFORMAT="%Y/%m/%d %H:%M:%S " 4 2017/06/28 18:51:45 echo $HISTTIMEFORMAT 5 2017/06/28 18:52:32 history [root@3 ~]# !4 echo $HISTTIMEFORMAT %Y/%m/%d %H:%M:%S [root@3 ~]# !HIST HISTSIZE=1000
按一次tab能够补全一个命令或参数(须要安装包bash-completion,并重启系统);按两次tab能够显示以某字母开头的全部命令或文件名。内存
语法: alias [命令别名]=[具体命令] 设置别名
取消别名:unalias [命令别名]ci
直接输入alias会显示系统全部的别名:
[root@3 ~]# 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 rm='rm -i' alias which='alias | /usr/bin/which --tty-only --read-alias --show-dot --show-tilde' [root@3 ~]#
系统别名存放在配置文件‘~/.bashrc’和‘ls /etc/profile.d/’下:
[root@3 ~]# cat !$ cat .bashrc # .bashrc # User specific aliases and functions alias rm='rm -i' alias cp='cp -i' alias mv='mv -i' # Source global definitions if [ -f /etc/bashrc ]; then . /etc/bashrc fi [root@3 ~]# ls /etc/profile.d/ 256term.csh colorgrep.sh lang.sh qt-graphicssystem.sh which2.sh 256term.sh colorls.csh less.csh vim.csh bash_completion.sh colorls.sh less.sh vim.sh colorgrep.csh lang.csh qt-graphicssystem.csh which2.csh
“>,>>,<,2>,2>>”
‘>’:输出重定向
‘>>’:追加剧定向
‘2>’:错误重定向
‘<’:输入重定向
使用‘>’命令时会将文件内原有内容删除。
[root@3 tmp]# echo 3xuelinux > 1.txt [root@3 tmp]# cat 1.txt 3xuelinux [root@3 tmp]# echo 3xu > 1.txt [root@3 tmp]# cat 1.txt 3xu ##################################### [root@3 tmp]# echo 3xu >> 1.txt [root@3 tmp]# cat 1.txt 3xu 3xu ##################################### [root@3 tmp]# lsaaa -bash: lsaaa: 未找到命令 [root@3 tmp]# lsaaa 2> 2.txt [root@3 tmp]# cat 2.txt -bash: lsaaa: 未找到命令 输入重定向:必须定向到(<左边)一个命令下 [root@3 tmp]# wc -l 1.txt “ wc -l”该命令用于查看文件行数 2 1.txt
[root@3 tmp]# ls {1,2}.txt aaaa.txt > 1.txt 2> 3.txt [root@3 tmp]# cat 1.txt 1.txt 2.txt [root@3 tmp]# cat 3.txt ls: 没法访问aaaa.txt: 没有那个文件或目录
说明: 使用ls命令查看 {1,2}.txt aaaa.txt,1.txt和2.txt文件存在,能够使用ls查看,aaaa.txt不存在,使用ls查看会报错,‘> 1.txt 2> 3.txt’意思是将正确信息保存到1.txt,将错误信息保存到3.txt。