shell学习笔记
一. shell介绍
1. shell是一个命令解释器,提供用户和机器之间的交互
2. 支持特定语法,比如逻辑判断.循环
3. 每个用户都可以有自己特定的shell
4. centos7默认shell为bash(Bounrne Agin Shell)
5. 还有zsh ksh
二 .命令历史
敲过的命令 就是命令历史 会存放在用户家目录下/root/.bash_history
history可以查看使用过的命令历史 最多为1000条
[root@qklinux-01 ~]# echo $HISTSIZE
1000
history –c 清空命令历史 不会清空删除配置文件/root/.bash_history
敲过的命令只有退出终端才会保存到文件去
变量HISTSIZE在/etc/profile中定义 在配置文件中修改HISTEIZE参数为5000
可以执行命令soure /etc/profile 马上生效
root@qklinux-01 ~]# echo $HISTSIZE
1000
[root@qklinux-01 ~]# history -c
[root@qklinux-01 ~]# vi /etc/profile
[root@qklinux-01 ~]# echo $HISTSIZE
1000
[root@qklinux-01 ~]# source /etc/profile
[root@qklinux-01 ~]# echo $HISTSIZE
5000
修改命令历史格式 可以以时间格式显示HISTTIMEFORMAT="%Y/%m/%d %H:%M:%S"
[root@qklinux-01 ~]# history
1 vi /etc/profile
2 echo $HISTSIZE
3 source /etc/progile
4 source /etc/profile
5 echo $HISTSIZE
6 history
[root@qklinux-01 ~]#
[root@qklinux-01 ~]# HISTTIMEFORMAT="%Y/%m/%d %H:%M:%S"
[root@qklinux-01 ~]# echo $HISTTIMEFORMAT
%Y/%m/%d %H:%M:%S
[root@qklinux-01 ~]# history
1 2019/04/01 20:37:48vi /etc/profile
2 2019/04/01 20:39:54echo $HISTSIZE
3 2019/04/01 20:40:12source /etc/progile
4 2019/04/01 20:40:23source /etc/profile
5 2019/04/01 20:40:26echo $HISTSIZE
6 2019/04/01 20:42:35history
7 2019/04/01 20:45:02HISTTIMEFORMAT="%Y/%m/%d %H:%M:%S"
8 2019/04/01 20:45:18ehco $
9 2019/04/01 20:45:39ehco $HISTTIMEFORMAT
10 2019/04/01 20:45:47echo $HISTTIMEFORMAT
11 2019/04/01 20:46:52history
这个格式只能在当前终端显示 想要生效需要编辑配置文件
#vim /etc/profile
HOSTNAME=`/usr/bin/hostname 2>/dev/null`
HISTSIZE=5000
HISTTIMEFORMAT="%Y/%m/%d %H:%M:%S"
root@qklinux-01 ~]# source !$
命令历史永久保存 不被别人删除运行chattr +a ~/.bash_history 只能追加不能删除
[root@qklinux-01 ~]# chattr +a ~/.bash_history
[root@qklinux-01 ~]# lsattr .bash_history
-----a---------- .bash_history
!!表示最后一条命令
[root@qklinux-01 ~]# pwd
/root
[root@qklinux-01 ~]# !!
pwd
/root
!n表示运行第几条命令
[root@qklinux-01 ~]# history
1 2019/04/01 20:37:48vi /etc/profile
2 2019/04/01 20:39:54echo $HISTSIZE
3 2019/04/01 20:40:12source /etc/progile
4 2019/04/01 20:40:23source /etc/profile
5 2019/04/01 20:40:26echo $HISTSIZE
6 2019/04/01 20:42:35history
7 2019/04/01 20:45:02HISTTIMEFORMAT="%Y/%m/%d %H:%M:%S"
8 2019/04/01 20:45:18ehco $
9 2019/04/01 20:45:39ehco $HISTTIMEFORMAT
10 2019/04/01 20:45:47echo $HISTTIMEFORMAT
11 2019/04/01 20:46:52history
12 2019/04/01 20:50:35vim /etc/profile
13 2019/04/01 20:53:00source /etc/profile
14 2019/04/01 20:53:43echo $HISTTIMEFORMAT
15 2019/04/01 20:53:47history
16 2019/04/01 20:56:32chattr +a ~/.bash_history
17 2019/04/01 20:57:27lsattr .bash_history
18 2019/04/01 20:57:39ls
19 2019/04/01 20:57:58pwd
20 2019/04/01 21:00:10ls
21 2019/04/01 21:00:43w
22 2019/04/01 21:01:06pwd
23 2019/04/01 21:02:05history
[root@qklinux-01 ~]# !22
pwd
/root
!echo 从命令历史倒着往上找 第一个以echo开头的命令
[root@qklinux-01 ~]# !echo
echo $HISTTIMEFORMAT
%Y/%m/%d %H:%M:%S
三.命令补全和别名
tab键 按一次可以补全命令 文件路径 按两次把所有命令和文件列出来 在centos7中支持参数自动补全 需要安装bash-completion包 安装完需要重启才能生效
[root@qklinux-01 ~]# yum install -y bash-completion
已加载插件:fastestmirror
Loading mirror speeds from cached hostfile
alias别名 给命令重新起个名字
[root@qklinux-01 ~]# alias restartnet='systemctl restart network.service'
[root@qklinux-01 ~]# restartnet
[root@qklinux-01 ~]# 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'
alias定义的位置
#vi .bashrc
#.bashrc
#User specific aliases and functions
alias rm='rm -i'
alias cp='cp -i'
alias mv='mv -i'
在用户及目录下.bashrc下面
在/etc/profile.d目录下的colorls.sh clorgrep.sh 这些脚本下定义
root@qklinux-01 ~]# ls /etc/profile.d/
256term.csh colorls.csh less.csh which2.csh
256term.sh colorls.sh less.sh which2.sh
bash_completion.sh csh.local sh.local
colorgrep.csh lang.csh vim.csh
colorgrep.sh lang.sh vim.sh
unalias取消别名
[root@qklinux-01 ~]# unalias restartnet
[root@qklinux-01 ~]# restartnet
-bash: restartnet: 未找到命令
四 通配符
通配符*
[root@qklinux-01 ~]# ls *.txt
1_sorft.txt 2.txt 3.txt 新建文本文档.txt
[root@qklinux-01 ~]# ls *txt
1_sorft.txt 2.txt 3.txt 新建文本文档.txt
[root@qklinux-01 ~]# ls *txt*
1_sorft.txt 1.txt~ 2.txt 3.txt 新建文本文档.txt
通配符? 表示任意的一个字符
[root@qklinux-01 ~]# ls ?.txt
2.txt 3.txt
[root@qklinux-01 ~]# touch 4.txt
[root@qklinux-01 ~]# touch 1.xtt
[root@qklinux-01 ~]# ls ?.txt
2.txt 3.txt 4.txt
[root@qklinux-01 ~]# touch a.txt
[root@qklinux-01 ~]# touch bb.txt
[root@qklinux-01 ~]# ls ?.txt
2.txt 3.txt 4.txt a.txt
方括号[] ls[0-9].txt表示0-9范围内任意的.txt文件
[root@qklinux-01 ~]# ls [0-3].txt
1.txt 2.txt 3.txt
[root@qklinux-01 ~]# ls [123].txt
1.txt 2.txt 3.txt
[root@qklinux-01 ~]# ls [23].txt
2.txt 3.txt
[root@qklinux-01 ~]# ls [0-9a-z].txt
1.txt 2.txt 3.txt 4.txt a.txt
[root@qklinux-01 ~]# ls [0-9a-zA-Z].txt
1. txt 2.txt 3.txt 4.txt a.txt
花括号{ } 表示括号内任意文件
[root@qklinux-01 ~]# ls {1,2,3}.txt
1.txt 2.txt 3.txt
[root@qklinux-01 ~]# ls {1,2,3,a}.txt
1. txt 2.txt 3.txt a.txt
五 .输入输出重定向
cat 1.txt > 2.txt 大于号>表示将前面的命令输出 直接输入到后面的文件里去 等于把前面的内容重定向到后面2.txt里面 之前的2.txt内容会被删除
[root@qklinux-01 ~]# cat 1.txt
[root@qklinux-01 ~]# echo "123" >> 1.txt
[root@qklinux-01 ~]# cat 1.txt
123
[root@qklinux-01 ~]# echo "234" >> 2.txt
[root@qklinux-01 ~]# cat 2.txt
234
[root@qklinux-01 ~]# cat 1.txt > 2.txt
[root@qklinux-01 ~]# cat 2.txt
123
cat 1.txt >> 2.txt 两个大于号>>表示追加 把前面的内容追加到后面去 不会删除原来的内容
[root@qklinux-01 ~]# cat 1.txt >> 2.txt
[root@qklinux-01 ~]# cat 2.txt
123
123
ls aaa.txt 2> a.txt 2大于号表示将错误信息写到后面文件去
ls aaa.txt 2>> a.txt 2>>表示将错误信息追加重定向
[root@qklinux-01 ~]# lsaaa
-bash: lsaaa: 未找到命令
[root@qklinux-01 ~]# lsaaa 2> a.txt
[root@qklinux-01 ~]# cat a.txt
-bash: lsaaa: 未找到命令
[root@qklinux-01 ~]# lsaaa 2>> a.txt
[root@qklinux-01 ~]# cat a.txt
-bash: lsaaa: 未找到命令
-bash: lsaaa: 未找到命令
把错误和正确的输出信息全部定向&> 追加&>>
[root@qklinux-01 ~]# ls [12].txt aaa.txt &> a.txt
[root@qklinux-01 ~]# cat a.txt
ls: 无法访问aaa.txt: 没有那个文件或目录
1.txt
2.txt
[root@qklinux-01 ~]# ls [12].txt aaa.txt &>> a.txt
[root@qklinux-01 ~]# cat a.txt
ls: 无法访问aaa.txt: 没有那个文件或目录
1.txt
2.txt
ls: 无法访问aaa.txt: 没有那个文件或目录
1.txt
2.txt
ls [12].txt aaa.txt > 1.txt 2>a.txt 表示将错误的输出到a.txt 正确的输出到1.txt
[root@qklinux-01 ~]# ls [12].txt aaa.txt > 1.txt 2>a.txt
[root@qklinux-01 ~]# cat 1.txt
1.txt
2.txt
[root@qklinux-01 ~]# cat a.txt
ls: 无法访问aaa.txt: 没有那个文件或目录
wc –l < 1.txt 输入重定向 把1.txt文件内容输入重定向到左边命令(wc –l查看文本文档行数)
[root@qklinux-01 ~]# wc -l < 1.txt
2
[root@qklinux-01 ~]# 2.txt < 1.txt
-bash: 2.txt: 未找到命令
六 管道符和作业控制
管道符| 表示把一个文件的输出内容传递给后面的命令
cat 1.txt |wc –l ;cat 1.txt |grep ‘aaa’
grep命令使用来过滤关键词的 只要文件里含有关键词 就会把这一行过滤出来
作业控制
ctrl+z 把正在执行的任务暂停
[root@qklinux-01 ~]# vi 1.txt
[1]+ 已停止 vi 1.txt
使用fg(foregroup)恢复任务
[root@qklinux-01 ~]# fg
vi 1.txt
1.txt
2.txt
暂停多个任务 用jobs命令查看后台运行任务
[root@qklinux-01 ~]# vim aa.txt
[2]+ 已停止 vim aa.txt
[root@qklinux-01 ~]# jobs
[1]- 已停止 vi 1.txt
[2]+ 已停止 vim aa.txt
重新调回fg(id) 也可以用bg 把任务放到后台运行
[root@qklinux-01 ~]# fg 2
vim aa.txt
[2]+ 已停止 vim aa.txt
[root@qklinux-01 ~]# bg 2
[2]+ vim aa.txt &
[root@qklinux-01 ~]# jobs
[1]- 已停止 vi 1.txt
[2]+ 已停止 vim aa.txt
vmstat 可以自动的不停地显示系统状态
sleep命[root@qklinux-01 ~]# sleep 1000
^Z
[1]+ 已停止 sleep 1000
[root@qklinux-01 ~]# jobs
[1]+ 已停止 sleep 1000
[root@qklinux-01 ~]# sleep 200
ZC^Z
[2]+ 已停止 sleep 200
[root@qklinux-01 ~]# fg
sleep 200
^Z
[2]+ 已停止 sleep 200
[root@qklinux-01 ~]# fg 1
sleep 1000
^Z
[1]+ 已停止 sleep 1000
[root@qklinux-01 ~]# bg 1
[1]+ sleep 1000 &
[root@qklinux-01 ~]# jobs
[1]- 运行中 sleep 1000 &
[2]+ 已停止 sleep 200令 比如 sleep 1000 表示系统暂停1000秒
后面加& 等于直接把任务调到后台运行
[root@qklinux-01 ~]# sleep 100 &
[1] 6533
[root@qklinux-01 ~]# jobs
[1]+ 运行中 sleep 100 &
七 shell变量
常用变量
查看系统常用环境变量env =号左边都是变量的名字
#env
XDG_SESSION_ID=3
HOSTNAME=qklinux-01
SELINUX_ROLE_REQUESTED=
TERM=xterm
SHELL=/bin/bash
HISTSIZE=5000
SSH_CLIENT=192.168.18.1 56031 22
SELINUX_USE_CURRENT_RANGE=
SSH_TTY=/dev/pts/1
USER=root
MAIL=/var/spool/mail/root
PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/bin
PWD=/root
LANG=zh_CN.UTF-8
SELINUX_LEVEL_REQUESTED=
HISTCONTROL=ignoredups
SHLVL=1
HOME=/root
LOGNAME=root
SSH_CONNECTION=192.168.18.1 56031 192.168.18.138 22
LESSOPEN=||/usr/bin/lesspipe.sh %s
XDG_RUNTIME_DIR=/run/user/0
_=/usr/bin/env
set 也可以查看变量 不仅可以查看系统内置的环境变量 也可以查看用户自定义变量
自定义变量a=111
[root@qklinux-01 ~]# a=111
[root@qklinux-01 ~]# echo $a
111
[root@qklinux-01 ~]# set |grep 111
_=111
a=111
变量名规则:字母 数字下划线 首位不能为数字
[root@qklinux-01 ~]# a1=2
[root@qklinux-01 ~]# echo $a1
2
[root@qklinux-01 ~]# a_1=3
[root@qklinux-01 ~]# echo $a_1
3
[root@qklinux-01 ~]# 1aa=2
-bash: 1aa=2: 未找到命令
变量值有特殊符号时需要用单引号括起来
[root@qklinux-01 ~]# a='a$bc'
[root@qklinux-01 ~]# echo $a
a$bc
变量的累加 稍微复杂的变量用双引号
[root@qklinux-01 ~]# a_1=3
[root@qklinux-01 ~]# echo $a_1
3
[root@qklinux-01 ~]# 1aa=2
-bash: 1aa=2: 未找到命令
[root@qklinux-01 ~]# a=a b c
-bash: b: 未找到命令
[root@qklinux-01 ~]# a=1
[root@qklinux-01 ~]# b=2
[root@qklinux-01 ~]# $a$b
-bash: 12: 未找到命令
[root@qklinux-01 ~]# echo $a$b
12
[root@qklinux-01 ~]# a='a$bc'
[root@qklinux-01 ~]# echo $a$b
a$bc2
[root@qklinux-01 ~]# c="a$bc"
[root@qklinux-01 ~]# echo $c
a
[root@qklinux-01 ~]# c="a$b"c
[root@qklinux-01 ~]# echo $c
a2c
[root@qklinux-01 ~]# c=‘a$b'c
> ^C
[root@qklinux-01 ~]# c='a$b'c
[root@qklinux-01 ~]# echo $c
a$bc
[root@qklinux-01 ~]# c=a"$b"c
[root@qklinux-01 ~]# echo $c
a2c
全局变量export b=2
使用echo ¥SSH_TTY 可以查看当前所在终端
[root@qklinux-01 ~]# w
11:04:43 up 1:19, 2 users, load average: 0.00, 0.01, 0.05
USER TTY FROM LOGIN@ IDLE JCPU PCPU WHAT
root pts/0 192.168.18.1 09:38 1:20m 0.07s 0.07s -bash
root pts/1 192.168.18.1 10:28 3.00s 0.15s 0.05s w
[root@qklinux-01 ~]# echo $SSH_TTY
/dev/pts/1
bash是shell变量的子bsah pstree查看
[root@qklinux-01 ~]# pstree
systemd─┬─NetworkManager───2*[{NetworkManager}]
├─VGAuthService
├─agetty
├─anacron
├─auditd───{auditd}
├─chronyd
├─crond
├─dbus-daemon───{dbus-daemon}
├─dnsmasq
├─firewalld───{firewalld}
├─lvmetad
├─master─┬─pickup
│ └─qmgr
├─polkitd───6*[{polkitd}]
├─rsyslogd───2*[{rsyslogd}]
├─sshd─┬─sshd───bash
│ └─sshd───bash───bash───pstree
├─systemd-journal
├─systemd-logind
├─systemd-udevd
├─tuned───4*[{tuned}]
└─vmtoolsd───{vmtoolsd}
在第二个bash下 看到第一个设置的bash环境变量在第二个不生效 需要设置全局变量全局变量时向下在子shell生效 运行子shell直接输入bash
[root@qklinux-01 ~]# export qkai=linux
[root@qklinux-01 ~]# echo $qkai
linux
[root@qklinux-01 ~]# bash
[root@qklinux-01 ~]# echo $qkai
linux
[root@qklinux-01 ~]# bash
[root@qklinux-01 ~]# pstree
systemd─┬─NetworkManager───2*[{NetworkManager}]
├─VGAuthService
├─agetty
├─anacron
├─auditd───{auditd}
├─chronyd
├─crond
├─dbus-daemon───{dbus-daemon}
├─dnsmasq
├─firewalld───{firewalld}
├─lvmetad
├─master─┬─pickup
│ └─qmgr
├─polkitd───6*[{polkitd}]
├─rsyslogd───2*[{rsyslogd}]
├─sshd─┬─sshd───bash
│ └─sshd───bash───bash───bash───bash───pstre+
├─systemd-journal
├─systemd-logind
├─systemd-udevd
├─tuned───4*[{tuned}]
└─vmtoolsd───{vmtoolsd}
unset变量 取消变量
[root@qklinux-01 ~]# unset qkai
[root@qklinux-01 ~]# echo $qkai
八 环境变量配置文件
环境变量配置文件分为 系统层次配置文件 用户层次配置文件
系统层次文件etc下的文件
/etc/profile 用户环境变量 交互 登录才执行
/etc/bashrc 用户不登录 执行shell就生效
用户层次文件 用户家目录下的文件
~/.bashrc
~/.bash_profile
系统层次的文件不用编辑 有需要时 编辑用户层次配置文件
…bash_profile 和source .bash_profile 作用是一样的 加载配置文件的配置
~/.bash_history 记录命令历史
~/.bash_logout 定义用户退出的时候需要做的一些操作
变量PS1 定义在/etc/bashrc 命令左边的字符串
[root@qklinux-01 ~]# cd /etc/sysconfig/network-scripts/
修改W为w
[root@qklinux-01 network-scripts]# echo $PS1
[\u@\h \W]\$
[root@qklinux-01 network-scripts]# PS1='[\u@\h \w]\$'
[root@qklinux-01 /etc/sysconfig/network-scripts]#cd
也可以去掉方括号
[root@qklinux-01 /tmp/qkai2]#PS1='\u@\h \w\$'
root@qklinux-01 /tmp/qkai2#
root@qklinux-01 /tmp/qkai2#PS1='<\u@\h \w\>$'
<root@qklinux-01 /tmp/qkai2\>$
带颜色显示
<root@qklinux-01/tmp/qkai2> #PS1='\[\033[01;32m\]\u@\h\[\033[00m\]:\[033[01;36m\]\w\[\033[00m\]\$'