8.1 shell 介绍、8.2 命令历史、8.3 命令补全与别名、8.4 通配符、8.5 输入输出重定向
发表于:2025-12-02 作者:千家信息网编辑
千家信息网最后更新 2025年12月02日,8.1 sehll 介绍什么是shellshell 是一个命令解释器,提供用户和机器之间交互支持特定的语法,比如逻辑判断,循环.每个用户都可以有自己特定的shell.centos7 默认的shell
千家信息网最后更新 2025年12月02日8.1 shell 介绍、8.2 命令历史、8.3 命令补全与别名、8.4 通配符、8.5 输入输出重定向
8.1 sehll 介绍
什么是shell
shell 是一个命令解释器,提供用户和机器之间交互
支持特定的语法,比如逻辑判断,循环.
每个用户都可以有自己特定的shell.
centos7 默认的shell 为bash( Bourne Agin shell )
还有zsh ,ksh等
8.2 命令历史
/root/.bash_history ;命令历史放置文件
[root@jzplinux-01 ~]# ls /root/.bash_history/root/.bash_history[root@jzplinux-01 ~]# cat !$cat /root/.bash_history
history ;查看系统存储的命令,最多可以存储1000条
[root@jzplinux-01 ~]# history | tail 995 make install 996 echo $? 997 ls /usr/local/apache2 998 init 0 999 ls /root/.bash_history 1000 cat $! 1001 ls /root/.bash_history 1002 cat /root/.bash_history 1003 history 1004 history | tail[root@jzplinux-01 ~]#
可以看到有1004 条,是因为还没有写入到文件中,暂时存在于内存中
echo $HISTSIZE ; 查看变量的值
[root@jzplinux-01 ~]# echo $HISTSIZE1000[root@jzplinux-01 ~]#
history -c ; 清空内存中的历史命令,并不会清空 /root/.bash_history 中保存的命令
[root@jzplinux-01 ~]# history -c[root@jzplinux-01 ~]# history 8 history [root@jzplinux-01 ~]# cat .bash_history | tailcd ..lscd httpd-2.2.32 lsmakeecho $?make installecho $?ls /usr/local/apache2init 0[root@jzplinux-01 ~]#
当退出当前终端时,内存中保存的命令会保存到 /root/.bash_history 文件中去
vim /etc/profile ; 在配置文件中修改 HISTTORY 变量的值
[root@jzplinux-01 ~]# vim /etc/profileHOSTNAME=`/usr/bin/hostname 2>/dev/null`HISTSIZE=2000if [ "$HISTCONTROL" = "ignorespace" ] ; then export HISTCONTROL=ignoreboth
source /etc/profile ; 重新加载文件之后,HISTSIZE 变量的值在会改变
[root@jzplinux-01 ~]# echo $HISTSIZE1000[root@jzplinux-01 ~]# source /etc/profile[root@jzplinux-01 ~]# echo $HISTSIZE2000[root@jzplinux-01 ~]#
HISTTIMEFORMAT="%Y/%m/%d %H:%M:%S " ; 设置命令历史显示格式
[root@jzplinux-01 ~]# HISTTIMEFORMAT="%Y/%m/%d %H:%M:%S " [root@jzplinux-01 ~]# echo $HISTTIMEFORMAT%Y/%m/%d %H:%M:%S[root@jzplinux-01 ~]# history 8 2017/06/25 12:27:07 history 9 2017/06/25 12:31:05 cat .bash_history 10 2017/06/25 12:31:14 cat .bash_history | tail 11 2017/06/25 12:35:08 cat /etc/profile 12 2017/06/25 13:10:00 vim /etc/profile 13 2017/06/25 13:14:38 echo $HISTORY 14 2017/06/25 13:15:34 echo $HISTTORY 15 2017/06/25 13:16:10 echo $HISTSIZE 16 2017/06/25 13:16:47 source /etc/profile 17 2017/06/25 13:17:13 echo $HISTSIZE 18 2017/06/25 13:19:55 history 19 2017/06/25 13:25:16 HISTTIMEFORMAT="%Y/%m/%d %H:%M:%S " 20 2017/06/25 13:25:37 echo $HISTTIMEFORMAT 21 2017/06/25 13:25:49 history[root@jzplinux-01 ~]#
只在当前终端生效,如果要在别的中终端生效,需要将变量加入到/etc/profile中
[root@jzplinux-01 ~]# vim /etc/profileHISTSIZE=2000HISTTIMEFORMAT="%Y/%m/%d %H:%M:%S "[root@jzplinux-01 ~]# source /etc/profile
chattr +a /root/.bash_history ; 给文件设置 a 权限,防止被删除
[root@jzplinux-01 ~]# chattr +a /root/.bash_history[root@jzplinux-01 ~]# lsattr /root/.bash_history-----a---------- /root/.bash_history[root@jzplinux-01 ~]#
!! 表示执行上一条命令,也就是命令历史中的最后一条命令
[root@jzplinux-01 ~]# ls111.txt 1.txt 1.txt.1 2.txt 3.txt anaconda-ks.cfg httpd-2.2.32.tar.gz[root@jzplinux-01 ~]# !!ls111.txt 1.txt 1.txt.1 2.txt 3.txt anaconda-ks.cfg httpd-2.2.32.tar.gz[root@jzplinux-01 ~]# w 13:43:44 up 5:12, 2 users, load average: 0.00, 0.01, 0.05USER TTY FROM LOGIN@ IDLE JCPU PCPU WHATroot pts/0 192.168.86.1 08:32 8.00s 0.21s 0.06s wroot pts/1 192.168.86.1 13:33 9:04 0.03s 0.03s -bash[root@jzplinux-01 ~]# !!w 13:43:46 up 5:12, 2 users, load average: 0.00, 0.01, 0.05USER TTY FROM LOGIN@ IDLE JCPU PCPU WHATroot pts/0 192.168.86.1 08:32 2.00s 0.16s 0.01s wroot pts/1 192.168.86.1 13:33 9:06 0.03s 0.03s -bash[root@jzplinux-01 ~]#
!n ;表示执行命令历史里的第n条命令,n 表示第几条命令
[root@jzplinux-01 ~]# history 8 2017/06/25 12:27:07 history 9 2017/06/25 12:31:05 cat .bash_history 10 2017/06/25 12:31:14 cat .bash_history | tail 11 2017/06/25 12:35:08 cat /etc/profile 12 2017/06/25 13:10:00 vim /etc/profile 13 2017/06/25 13:14:38 echo $HISTORY 14 2017/06/25 13:15:34 echo $HISTTORY 15 2017/06/25 13:16:10 echo $HISTSIZE 16 2017/06/25 13:16:47 source /etc/profile 17 2017/06/25 13:17:13 echo $HISTSIZE 18 2017/06/25 13:19:55 history 19 2017/06/25 13:25:16 HISTTIMEFORMAT="%Y/%m/%d %H:%M:%S " 20 2017/06/25 13:25:37 echo $HISTTIMEFORMAT 21 2017/06/25 13:25:49 history 22 2017/06/25 13:31:12 vim /etc/profile 23 2017/06/25 13:33:21 source /etc/profile 24 2017/06/25 13:39:08 chattr +a /root/.bash_history 25 2017/06/25 13:39:38 lsattr /root/.bash_history 26 2017/06/25 13:43:18 ls 27 2017/06/25 13:43:40 W 28 2017/06/25 13:43:43 w 29 2017/06/25 13:45:46 history[root@jzplinux-01 ~]# !17echo $HISTSIZE2000[root@jzplinux-01 ~]#
!+字符串 ;表示执行以字符串开头的,history中最近的一条命令
[root@jzplinux-01 ~]# history | tail -n 7 29 2017/06/25 13:45:46 history 30 2017/06/25 13:46:00 echo $HISTSIZE 31 2017/06/25 13:50:11 w 32 2017/06/25 13:53:03 history | tail 33 2017/06/25 13:53:18 history | tail -n 3 34 2017/06/25 13:53:32 history | tail -n3 35 2017/06/25 13:53:47 history | tail -n 7[root@jzplinux-01 ~]# [root@jzplinux-01 ~]# !echoecho $HISTSIZE2000[root@jzplinux-01 ~]# !ww 13:50:11 up 5:19, 2 users, load average: 0.00, 0.01, 0.05USER TTY FROM LOGIN@ IDLE JCPU PCPU WHATroot pts/0 192.168.86.1 08:32 3.00s 0.17s 0.01s wroot pts/1 192.168.86.1 13:33 15:31 0.03s 0.03s -bash[root@jzplinux-01 ~]#
8.3 命令补全与别名
tab 键命令补全
敲一下 补全命令或路径
敲两下 列出当前目录下的所有文件
yum install -y bash-completion ;安装包之后tab键可以补全命令的参数
安装完之后需要重新启动系统,才能生效
[root@jzplinux-01 ~]# rpm -q bash-completionbash-completion-2.1-6.el7.noarch[root@jzplinux-01 ~]# systemctl restart network
alias restartnet='systemctl restart network.service' ; 设置别名
[root@jzplinux-01 ~]# alias restartnet='systemctl restart network.service'[root@jzplinux-01 ~]# restartnet[root@jzplinux-01 ~]#
alias ; 列出系统所有的别名
[root@jzplinux-01 ~]# aliasalias 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@jzplinux-01 ~]#
/root/.bashrc /etc/profile.d ; 定义alias 的文件
/root/.bashrc
[root@jzplinux-01 ~]# cat /root/.bashrc# .bashrc# User specific aliases and functionsalias rm='rm -i'alias cp='cp -i'alias mv='mv -i'# Source global definitionsif [ -f /etc/bashrc ]; then . /etc/bashrcfi[root@jzplinux-01 ~]#
/etc/profile.d 下的脚本文件中
[root@jzplinux-01 ~]# ls /etc/profile.d256term.csh colorgrep.csh colorls.sh less.csh vim.sh356term.sh colorgrep.sh lang.csh less.sh which3.cshbash_completion.sh colorls.csh lang.sh vim.csh which3.sh
unalias restartnet ; 取消自定义的别名
[root@jzplinux-01 ~]# unalias restartnet[root@jzplinux-01 ~]# restartnet-bash: restartnet: 未找到命令[root@jzplinux-01 ~]#
8.4 通配符
* 任意个任意字符
[root@jzplinux-01 ~]# ls *.txt111.txt 1.txt 2.txt 3.txt[root@jzplinux-01 ~]# ls *txt*111.txt 1.txt 1.txt.1 2.txt 3.txt[root@jzplinux-01 ~]# ls 1*111.txt 1.txt 1.txt.1[root@jzplinux-01 ~]#
? ;表示一个任意字符
[root@jzplinux-01 ~]# ls ?.txt1.txt 2.txt 3.txt[root@jzplinux-01 ~]#
ls [1-9].txt ; 列出包含数字的文件 [0-9],[123],[23],[a-z],[A-Z],[0-9a-zA-Z]
[root@jzplinux-01 ~]# ls [0-9].txt1.txt 2.txt 3.txt[root@jzplinux-01 ~]# le [23].txt-bash: le: 未找到命令[root@jzplinux-01 ~]# ls [23].txt2.txt 3.txt[root@jzplinux-01 ~]#
ls {1,2,3}.txt ; 列出包含花括号当中的文件
[root@jzplinux-01 ~]# ls {1,2}.txt1.txt 2.txt[root@jzplinux-01 ~]# ls {1,2,3}.txt1.txt 2.txt 3.txt[root@jzplinux-01 ~]#8.5 输入输出重定向
cat 1.txt > 2.txt ;重定向,将前一个命令的结果重定向到后面的文件中
[root@jzplinux-01 ~]# cat 2.txt1234567890[root@jzplinux-01 ~]# cat 1.txtchrony:x:997:995::/var/lib/chrony:/sbin/nologinaming:x:1000:1000::/home/aming:/bin/bashuser1:x:1001:1001::/home/user1:/bin/bashtcpdump:x:72:72::/:/sbin/nologinnginx:x:996:994:Nginx web server:/var/lib/nginx:/sbin/nologin[root@jzplinux-01 ~]# cat 1.txt > 2.txt[root@jzplinux-01 ~]# cat 2.txtchrony:x:997:995::/var/lib/chrony:/sbin/nologinaming:x:1000:1000::/home/aming:/bin/bashuser1:x:1001:1001::/home/user1:/bin/bashtcpdump:x:72:72::/:/sbin/nologinnginx:x:996:994:Nginx web server:/var/lib/nginx:/sbin/nologin[root@jzplinux-01 ~]#
cat 1.txt >> 2.txt ; 追加重定向,将前面命令的输出追加到后面的文件中
[root@jzplinux-01 ~]# cat 1.txtchrony:x:997:995::/var/lib/chrony:/sbin/nologinaming:x:1000:1000::/home/aming:/bin/bashuser1:x:1001:1001::/home/user1:/bin/bashtcpdump:x:72:72::/:/sbin/nologinnginx:x:996:994:Nginx web server:/var/lib/nginx:/sbin/nologin[root@jzplinux-01 ~]# cat 2.txtchrony:x:997:995::/var/lib/chrony:/sbin/nologinaming:x:1000:1000::/home/aming:/bin/bashuser1:x:1001:1001::/home/user1:/bin/bashtcpdump:x:72:72::/:/sbin/nologinnginx:x:996:994:Nginx web server:/var/lib/nginx:/sbin/nologin[root@jzplinux-01 ~]# cat 1.txt >> 2.txt[root@jzplinux-01 ~]# cat 2.txtchrony:x:997:995::/var/lib/chrony:/sbin/nologinaming:x:1000:1000::/home/aming:/bin/bashuser1:x:1001:1001::/home/user1:/bin/bashtcpdump:x:72:72::/:/sbin/nologinnginx:x:996:994:Nginx web server:/var/lib/nginx:/sbin/nologinchrony:x:997:995::/var/lib/chrony:/sbin/nologinaming:x:1000:1000::/home/aming:/bin/bashuser1:x:1001:1001::/home/user1:/bin/bashtcpdump:x:72:72::/:/sbin/nologinnginx:x:996:994:Nginx web server:/var/lib/nginx:/sbin/nologin[root@jzplinux-01 ~]#
lsaa 2> a.txt ; 错误重定向
[root@jzplinux-01 ~]# touch a.txt[root@jzplinux-01 ~]# lsaaa -bash: lsaaa: 未找到命令[root@jzplinux-01 ~]# lsaaa 2> a.txt[root@jzplinux-01 ~]# cat a.txt-bash: lsaaa: 未找到命令[root@jzplinux-01 ~]#
lsaa 2>> a.txt ; 追加错误重定向
[root@jzplinux-01 ~]# cat a.txt-bash: lsaaa: 未找到命令[root@jzplinux-01 ~]# lsaaa 2>> a.txt[root@jzplinux-01 ~]# lsaaa 2>> a.txt[root@jzplinux-01 ~]# cat a.txt-bash: lsaaa: 未找到命令-bash: lsaaa: 未找到命令-bash: lsaaa: 未找到命令[root@jzplinux-01 ~]#
ls [23].txt aaa.txt &> a.txt ; &> 相当于 2> + >
[root@jzplinux-01 ~]# ls [23].txt aaa.txt &> a.txt[root@jzplinux-01 ~]# cat a.txtls: 无法访问aaa.txt: 没有那个文件或目录2.txt3.txt[root@jzplinux-01 ~]#
ls [23].txt aaa.txt &>> a.txt ; &>> 相当于 2>> + >>
[root@jzplinux-01 ~]# ls [23].txt aaa.txt &>> a.txt[root@jzplinux-01 ~]# cat a.txtls: 无法访问aaa.txt: 没有那个文件或目录2.txt3.txtls: 无法访问aaa.txt: 没有那个文件或目录2.txt3.txt[root@jzplinux-01 ~]#
ls [23].txt aaa.txt > 1.txt 2> a.txt ; 将正确输出和错误输出到不同的文件中
[root@jzplinux-01 ~]# ls [23].txt aaa.txt > 1.txt 2> a.txt[root@jzplinux-01 ~]# cat 1.txt2.txt3.txt[root@jzplinux-01 ~]# cat a.txtls: 无法访问aaa.txt: 没有那个文件或目录[root@jzplinux-01 ~]#
wc -l <2.txt ; 输入重定向,左边一定是一个命令,不能是文件
[root@jzplinux-01 ~]# cat 2.txtchrony:x:997:995::/var/lib/chrony:/sbin/nologinaming:x:1000:1000::/home/aming:/bin/bashuser1:x:1001:1001::/home/user1:/bin/bashtcpdump:x:72:72::/:/sbin/nologinnginx:x:996:994:Nginx web server:/var/lib/nginx:/sbin/nologinchrony:x:997:995::/var/lib/chrony:/sbin/nologinaming:x:1000:1000::/home/aming:/bin/bashuser1:x:1001:1001::/home/user1:/bin/bashtcpdump:x:72:72::/:/sbin/nologinnginx:x:996:994:Nginx web server:/var/lib/nginx:/sbin/nologin[root@jzplinux-01 ~]# wc -l <2.txt10[root@jzplinux-01 ~]#
命令
文件
历史
目录
别名
输出
变量
字符
内存
系统
终端
错误
输入
字符串
用户
存储
通配符
不同
之间
也就是
数据库的安全要保护哪些东西
数据库安全各自的含义是什么
生产安全数据库录入
数据库的安全性及管理
数据库安全策略包含哪些
海淀数据库安全审计系统
建立农村房屋安全信息数据库
易用的数据库客户端支持安全管理
连接数据库失败ssl安全错误
数据库的锁怎样保障安全
全球安全研究信息数据库
儿童网络安全手指舞
面向对象 数据库 设计
财经网站和经济研究数据库网站
北京软件开发定制外包
人保财险软件开发中心总经理
web查数据库响应慢
数据库启动程序路径
厚道互联网科技有限公司怎么样
网络安全留言简短
dede的数据库位置
上海北大青鸟软件开发培训中心
成都商业银行软件开发工资
软件开发去哪里培训好
无服务器怎么改变应用开发
海淀区软件开发
衡水市网络安全宣传周
方舟怎么设置服务器
联想服务器资质
网络安全课程任务
网络安全优化社区服务
互联网科技创新产品
手机怎么删除网络安全证书
10000多人的服务器
陕西服务器虚拟化迁移云服务器
杭州专业软件开发哪家便宜
mysql数据库安装教程
杭州项目软件开发需要多少钱
长沙学习软件开发需要多少钱
E4a数据库查询不出来