shell基础,命令历史,命令补全,别名alias,通配符,输入输出重定向
shell基础
shell
是一个命令解释器,提供用户和机器之间的交互,支持特定语法,比如逻辑判断,循环等。
每个用户都可以有自己特性的shell,centos7默认shell为bash(bourne agin shell),还有zsh,ksh等,每种shell支持的特性不尽相同。
命令历史
history命令 查看命令历史。
~/.bash_history 用户命令历史记录文件
在正常退出终端,命令历史可保存在此文件,非正常退出终端,保存不全
/etc/profile history命令的配置文件
echo $HISTSIZE 查看命令历史最大记录值,默认HISTSIZE=1000
用history查看命令历史,结果可能会大于设定的最大值,大于最大值的命令历史暂存在内存中,未写入 .bash_history文件。
echo $HISTTIMEFORMAT 查看时间变量,默认无此变量,需要添加
vim /etc/profile 进入配置文件
修改:
HISTSIZE=5000 修改变量HISTSIZE,设定最大值为5000
修改:
HISTTIMEFORMAT="%Y/%m/%d %H:%M:%S " 定义时间变量,记录每条命令的操作时间
source /etc/profile 加载配置文件,修改profile后需要加载使修改生效。
history -c 清空内存中的命令历史
不会删除 .bash_history文件中的命令历史。
chattr +a ~/.bash_history 永久保存命令历史,只许追加不许删不受HISTSIZE值限制
快捷用法:
!! 最后一条命令
!n n= 命令历史号,指定使用历史号为n的命令
!word 使用由后向前查找最后一条以word开头的命令
例:
~
~
省略
999 cd /usr/local/src
1000 ls 可看到历史命令记录号大于默认的1000,这些记录暂存于内存未写入 .bash_history文件
~
~
省略
1011 history
1012 ls /home/
1013 ls /home/ELON/.bash_history
1014 cat /home/ELON/.bash_history
1015 echo $HISTSIZE 查看变量HISTSIZE最大值,此变量规定了最大记录号
1016 cat /root/.bash_history 查看root用户的命令历史记录文件
1017 history
cat /root/.bash_history
ls /home
init 0
mount /dev/cdrom /mnt
cd /mnt/Packages/
yum -qa |grep pcre
yum list |grep pcre
yum install -y pcre-devel.x86_64
[[email protected] ~]#
echo $HISTSIZE
1000 记录号为1000,默认值
[[email protected] ~]#
所以此处变量值为空
[[email protected] ~]# vim /etc/profile 配置history命令的配置文件

找到:HISTSIZE=1000,将其改为:HISTSIZE=5000,并在下一行插入:HISTTIMEFORMAT="%Y/%m/%d %H:%M:%S "

1000
5000
%Y/%m/%d %H:%M:%S
[[email protected] ~]#
~
~
省略
1013 2018/04/21 01:51:37 ls /home/ELON/.bash_history
1014 2018/04/21 01:51:47 cat /home/ELON/.bash_history
1015 2018/04/21 01:53:38 echo $HISTSIZE
1016 2018/04/21 01:54:33 cat /root/.bash_history
1017 2018/04/21 03:18:22 history
1018 2018/04/21 03:25:03 cat /root/.bash_history
1019 2018/04/21 03:34:07 echo $HISTSIZE
1020 2018/04/21 03:49:09 echo $HISTTIMEFORMAT
1021 2018/04/21 03:53:09 vim /etc/profile
1022 2018/04/21 04:03:23 echo $HISTSIZE
1023 2018/04/21 04:04:01 echo $HISTTIMEFORMAT
1024 2018/04/21 04:07:03 source /etc/profile
1025 2018/04/21 04:07:15 echo $HISTSIZE
1026 2018/04/21 04:07:23 echo $HISTTIMEFORMAT
1027 2018/04/21 04:10:18 history
25 2018/04/21 04:14:02 history
history
25 2018/04/21 04:14:02 history
[[email protected] ~]#
命令补全及别名
tab键
输入命令或参数时按一次补全,按两次查看以前面内容为前缀的命令或参数,也可用于文件或目录
centos7,支持参数补全,centos6只支持命令补全
参数补全,需安装 bash-completion 默认未安装,安装完需要重启系统才生效
~/.bashrc 用户自定义的alias存于用户家目录下此文件
/etc/profile.d/ 系统自带的alias在此目录下
unalias aliasname 取消alias
例:
已加载插件:fastestmirror
base | 3.6 kB 00:00:00
epel/x86_64/metalink | 8.1 kB 00:00:00
epel | 4.7 kB 00:00:00
extras | 3.4 kB 00:00:00
updates | 3.4 kB 00:00:00
(1/2): epel/x86_64/updateinfo | 916 kB 00:00:00
(2/2): epel/x86_64/primary_db | 6.3 MB 00:00:01
Loading mirror speeds from cached hostfile
* epel: mirrors.ustc.edu.cn
正在解决依赖关系
--> 正在检查事务
---> 软件包 bash-completion.noarch.1.2.1-6.el7 将被 安装
--> 解决依赖关系完成
依赖关系解决
===================================================================================================================
Package 架构 版本 源 大小
===================================================================================================================
正在安装:
bash-completion noarch 1:2.1-6.el7 base 85 k
事务概要
===================================================================================================================
安装 1 软件包
总下载量:85 k
安装大小:259 k
Downloading packages:
bash-completion-2.1-6.el7.noarch.rpm | 85 kB 00:00:00
Running transaction check
Running transaction test
Transaction test succeeded
Running transaction
正在安装 : 1:bash-completion-2.1-6.el7.noarch 1/1
验证中 : 1:bash-completion-2.1-6.el7.noarch 1/1
已安装:
bash-completion.noarch 1:2.1-6.el7
完毕!
安装完,重启服务器
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 restnet='systemctl restart network.service' 这是我们定义的别名为restnet的命令
alias rm='rm -i'
alias which='alias | /usr/bin/which --tty-only --read-alias --show-dot --show-tilde'
[[email protected] ~]#
# .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
256term.csh bash_completion.sh colorgrep.sh colorls.sh lang.csh less.csh PackageKit.sh vim.sh which2.csh
256term.sh colorgrep.csh colorls.csh flatpak.sh lang.sh less.sh vim.csh vte.sh which2.sh
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'
[[email protected] ~]#
通配符
* 表示不分字符,不分数量
? 表示一个任意的字符
[ ] 表示范围中的每一个
{ ,} 表示指定的每一个
例:
[[email protected] ~]# ls
1.txt 2.txt 3.txt.new anaconda-ks.cfg A.txt C.txt initial-setup-ks.cfg
1.txt.new 2.txt.new 456 a.txt c.txt home
1.txt 2.txt a.txt A.txt c.txt C.txt
1.txt 1.txt.new 2.txt 2.txt.new 3.txt.new a.txt A.txt c.txt C.txt
1.txt 1.txt.new
1.txt 2.txt a.txt A.txt c.txt C.txt
1.txt 2.txt
1.txt 2.txt a.txt A.txt c.txt C.txt
1.txt 2.txt
1.txt 2.txt A.txt c.txt
[[email protected] ~]#
输入输出重定向
> 正确输出 >> 正确追加 2> 错误输出 2>> 错误追加 < 输入重定向
>+2>== &> 正确输出,错误输出 >>+2>>==&>> 正确追加,错误追加
command [file1] >file2 用>前面命令输出的内容覆盖后面的文件内容
command [file1] >>file2 将>>前面命令输出的内容追加到后面的文件里
command [file1] > file2 2>file3 正确输出写入file2 错误输出写入file3
command < file 将<右边文件的内容输入到<左边命令里,只能用于命令
例:
1234 内容已被覆盖
1234
1234
[[email protected] ~]# ls
1.txt 2.txt 3.txt 456 anaconda-ks.cfg a.txt b.txt c.txt d.txt home initial-setup-ks.cfg
用前面命令的正确输出替换b.txt文件内容,用前面命令的错误输出替换c.txt文件内容
1.txt
ls: 无法访问A.txt: 没有那个文件或目录
[[email protected] ~]#
[[email protected] ~]# cat d.txt
ls: 无法访问A.txt: 没有那个文件或目录
1.txt
[[email protected] ~]# ls 1.txt A.txt >>b.txt 2>>c.txt
将前面命令的正确输出追加进b.txt文件,将前面命令的错误输出追加进c.txt文件
[[email protected] ~]# cat b.txt
1.txt
1.txt
ls: 无法访问A.txt: 没有那个文件或目录
ls: 无法访问A.txt: 没有那个文件或目录
[[email protected] ~]# cat d.txt
ls: 无法访问A.txt: 没有那个文件或目录
1.txt
ls: 无法访问A.txt: 没有那个文件或目录
1.txt
[[email protected] ~]# wc -l < d.txt 统计右边文件输入的内容行数
4
[[email protected] ~]#