Shell 基础知识
shell是一个命令解释器,提供用户和机器之间的交互
支持特定语法,比如逻辑判断,循环。
命令历史
执行过的命令linux都会记录,预设可以记录1000条历史命令,这些命令保存在用户家目录的.bash_history文件中,但只有用户正常退出当前shell时,在当前shell运行的命令才会保存至.bash_history文件中
[root[@localhost](https://my.oschina.net/u/570656) ~]# echo $HISTSIZE
1000
[root[@localhost](https://my.oschina.net/u/570656) ~]#
!! 连续2个!表示执行上一个命令
[root[@localhost](https://my.oschina.net/u/570656) ~]# pwd
/root
[root[@localhost](https://my.oschina.net/u/570656) ~]# !!
pwd
/root
[root[@localhost](https://my.oschina.net/u/570656) ~]#
!字符串 表示执行命令历史中最近一次 以p开头的命令
[root@localhost ~]# !p
pwd
/root
[root@localhost ~]#
!n n--对应历史命令中的行数
[root@localhost ~]# !138
history |tail
132 2018/05/30 06:41:37HISTTIMEFORMAT="%Y/%m/%d %H:%M:%S"
133 2018/05/30 06:41:39echo HISTTIMEFORMAT
134 2018/05/30 06:41:51vim /etc/profile
135 2018/05/30 06:42:20source /etc/profile
136 2018/05/30 06:42:23history
137 2018/05/30 06:42:29history -tail
138 2018/05/30 06:42:36history |tail
139 2018/05/30 06:45:57chattr +a ~/.bash_history
140 2018/05/30 06:50:06history -tail
141 2018/05/30 06:50:11history |tail
[root@localhost ~]#
history -c 删除当前shell的历史命令,但不包括.bash_history文件文件中的命令
vim /etc/profile 修改配置文件中的历史命令保存数量
[root@localhost ~]# echo $HISTSIZE
1000
[root@localhost ~]# vim /etc/profile
[root@localhost ~]# source /etc/profile
[root@localhost ~]# echo $HISTSIZE
2000
[root@localhost ~]#
历史命令加上时间
HISTTIMEFORMAT='%Y/%m/%d %H:%M:%S'
[root@localhost ~]# history |tail
129 2018/05/30 06:40:37HISTTIMEFORMAT='%Y/%m/%d %H:%M:%S'
130 2018/05/30 06:40:43echo !
131 2018/05/30 06:40:59echo HISTTIMEFORMAT
132 2018/05/30 06:41:37HISTTIMEFORMAT="%Y/%m/%d %H:%M:%S"
133 2018/05/30 06:41:39echo HISTTIMEFORMAT
134 2018/05/30 06:41:51vim /etc/profile
135 2018/05/30 06:42:20source /etc/profile
136 2018/05/30 06:42:23history
137 2018/05/30 06:42:29history -tail
138 2018/05/30 06:42:36history |tail
[root@localhost ~]#
历史命令目录加上 a权限。只限追加
[root@localhost ~]# chattr +a ~/.bash_history
如没有正常退出,命令则保存不全
命令补全和别名
tab键
按一次tab键 可以补全一个指令,路径,或者文件名。
连续按两次tab键,系统则会把所有的命令或者文件名都列出来。
别名
自定义别名
[root@localhost ~]# alias lan='pwd'
[root@localhost ~]# lan
/root
[root@localhost ~]#
解除别名
[root@localhost ~]# unalias lan
[root@localhost ~]# lan
-bash: lan: 未找到命令
[root@localhost ~]#
列出系统预设的别名
[root@localhost ~]# 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@localhost ~]#
通配符
匹配后缀为.txt的文件
[root@localhost ~]# ls *.txt
1.txt
[root@localhost ~]#
匹配1开头的文件或者目录
[root@localhost ~]# ls 1*
1 1.txt
[root@localhost ~]#
? 匹配任意一个字符
[root@localhost ~]# ls ?.txt
1.txt
[root@localhost ~]# ls
1 11.txt 1.txt 666 anaconda-ks.cfg
[root@localhost ~]#
由于只匹配任意一个字符,所以11.txt没有匹配到
匹配符合条件的内容
[root@localhost ~]# ls [0-9].txt
1.txt 2.txt 3.txt
[root@localhost ~]# ls [12].txt
1.txt 2.txt
[root@localhost ~]#
[0-9] 匹配任意数字
[12] 匹配1或者2
[root@localhost ~]# ls [a-z].txt
a.txt
[root@localhost ~]#
[a-z]匹配任意字母
[root@localhost ~]# ls {1,4}.txt
ls: 无法访问4.txt: 没有那个文件或目录
1.txt
[root@localhost ~]# ls {1,3}.txt
1.txt 3.txt
[root@localhost ~]#
{1,4}匹配1或者4
输入输出重定向
> 覆盖
>> 追加
2> 错误命令覆盖操作
2>> 错误命令追加操作
&> 正确及错误命令同时存在并覆盖操作
&>> 正确及错误命令同时存在并追加操作
覆盖演示:
[root@localhost ~]# cat 2.txt
345
[root@localhost ~]# cat 1.txt
123
[root@localhost ~]# cat 1.txt > 2.txt
[root@localhost ~]# cat 2.txt
123
[root@localhost ~]#
追加演示:
[root@localhost ~]# cat 1.txt >> 2.txt
[root@localhost ~]# cat 2.txt
123
123
[root@localhost ~]#
错误命令追加及覆盖演示
[root@localhost ~]# pwddd
-bash: pwddd: 未找到命令
[root@localhost ~]# pwddd 2> a.txt
[root@localhost ~]# cat a.txt
-bash: pwddd: 未找到命令
[root@localhost ~]# pwddd 2>> a.txt
[root@localhost ~]# cat a.txt
-bash: pwddd: 未找到命令
-bash: pwddd: 未找到命令
[root@localhost ~]#
正确和错误命令同时存在的追加及覆盖演示
[root@localhost ~]# ls [a-z].txt aaa.txt &> 2.txt
[root@localhost ~]# cat 2.txt
ls: 无法访问aaa.txt: 没有那个文件或目录
a.txt
[root@localhost ~]# ls [a-z].txt aaa.txt &>> 2.txt
[root@localhost ~]# cat 2.txt
ls: 无法访问aaa.txt: 没有那个文件或目录
a.txt
ls: 无法访问aaa.txt: 没有那个文件或目录
a.txt
[root@localhost ~]#
正确及错误命令分开存储
[root@localhost ~]# ls [a-z].txt aaa.txt > 2.txt 2>3.txt
[root@localhost ~]# cat 2.txt
a.txt
[root@localhost ~]# cat 3.txt
ls: 无法访问aaa.txt: 没有那个文件或目录
[root@localhost ~]#
管道符和作业控制
| 管道符 把前面一个命令的结果 通过管道符作为后一个命令的输入
[root@localhost ~]# cat /etc/passwd | wc -l
21
[root@localhost ~]#
作业控制
当运行进程时,可以使用(ctrl + z 组合键),然后使用fg命令恢复它,或是利用bg使它到后台运行,此外,你也可以使用(ctrl + c 组合键)终止。
[root@localhost ~]# vim 1.txt
[1]+ 已停止 vim 1.txt
[root@localhost ~]#
[root@localhost ~]# fg
vim 1.txt
[1]+ 已停止 vim 1.txt
[root@localhost ~]#
[root@localhost ~]# bg
[1]+ vim 1.txt &
[root@localhost ~]#
jobs查看任务
[root@localhost ~]# jobs
[1] 已停止 vim 1.txt
[2]- 已停止 vim 2.txt
[3]+ 已停止 vim 1.txt
[root@localhost ~]#
使用 & 把任务放到后台运行,会显示pid信息。
[root@localhost ~]# vmstat 1 > ./1.txt &
[4] 2439
[root@localhost ~]#
变量
变量既是使用一个交简单的字符串来替代某些具有特殊意义的设定以及数据。
env 可列出系统预设的全部系统变量
[root@localhost ~]# env | tail
LANG=zh_CN.UTF-8
SELINUX_LEVEL_REQUESTED=
HISTCONTROL=ignoredups
SHLVL=1
HOME=/root
LOGNAME=root
SSH_CONNECTION=192.168.1.150 49292 192.168.1.180 22
LESSOPEN=||/usr/bin/lesspipe.sh %s
XDG_RUNTIME_DIR=/run/user/0
_=/usr/bin/env
[root@localhost ~]#
常见环境变量
HOSTNAME:表示主机的名称
SHELL:表示当前用户的shell类型
HISTSIZE:表示历史命令记录数
MAIL:表示当前用户的邮件存放目录
PATH:该变量决定了shell将到哪些目录中寻找命令及程序
PWD:表示当前目录
LANG:这是与语言相关的环境变量,多语言环境可修改此变量
HOME:表示当前用户的家目录
LOGINNAME:表示当前用户的登录名
env显示的只是环境变量,可以使用set命令把系统预设的全部变量显示出来
set不仅可以显示系统预设的变量,还可以显示用户自定义的变量
[root@localhost ~]# a=111
[root@localhost ~]# set | tail -n 3
_=
a=111
colors=/root/.dircolors
[root@localhost ~]#
变量命名规则
设定变量的格式为a=b,其中a为变量名,b为变量的内容,等号两边不能有空格;
变量名只能由字母,数字以及下划线组成,而且不能以数字开头;
当变量内容带有特殊字符(如空格)时,需要加上单引号。示例如下:
[root@localhost ~]# myname='lan tern'
[root@localhost ~]# echo $myname
lan tern
[root@localhost ~]#
pstree
pstree会把linux系统中所有进程以树形结构显示出来
[root@localhost yum.repos.d]# pstree
systemd─┬─NetworkManager───2*[{NetworkManager}]
├─auditd───{auditd}
├─chronyd
├─crond
├─dbus-daemon───{dbus-daemon}
├─httpd───5*[httpd]
├─login───bash
├─master─┬─pickup
│ └─qmgr
├─nginx───nginx
├─polkitd───5*[{polkitd}]
├─rsyslogd───2*[{rsyslogd}]
├─sshd───sshd───bash─┬─pstree
│ ├─3*[vim]
│ └─vmstat
├─systemd-journal
├─systemd-logind
├─systemd-udevd
└─tuned───4*[{tuned}]
[root@localhost yum.repos.d]#
export声明变量
在父shell中设定后,进入子shell时,该变量是不会生效的。如果想让子shell生效,则需要使用export命令
[root@localhost ~]# echo $myname
lan tern
[root@localhost ~]# bash
[root@localhost ~]# echo $myname
[root@localhost ~]# exit
exit
[root@localhost ~]# export myname
[root@localhost ~]# echo $myname
lan tern
[root@localhost ~]# bash
[root@localhost ~]# echo $myname
lan tern
[root@localhost ~]#
unset 取消变量
[root@localhost ~]# echo $myname
lan tern
[root@localhost ~]# unset myname
[root@localhost ~]# echo $myname
[root@localhost ~]#
系统环境变量配置文件
** 系统级别的配置文件:**
/etc/profile:这几个文件预设了几个重要的变量,例如PATH,USER,LOGINNAME,MAIL,INPUTRC,HOSTNAME,HISTSIZE,UMASK等
/etc/bashrc:这个文件主要预设umask以及ps1.
[root@localhost ~]# echo $PS1
[\u@\h \W]\$
[root@localhost ~]#
\u指用户 \h指主机名 \w指当前目录 \$ 指字符#(如普通用户则显示$)
用户目录下的隐藏的配置文件:
.bash_profile:该文件定义了用户的个人化路径与环境变量的文件名称。每个用户都可以使用该文件输入专属于自己的shell信息,当用户登录时,该文件仅仅执行一次。
.bashrc:该文件包含专属于自己的shell的bash信息,当登录或每次打开新的shell时,该文件会被读取。
.bash_history:该文件用户记录历史命令
.bash_logout:当退出shell时,会执行该文件。
bashrc和bash_profile的区别 http://ask.apelearn.com/question/7719