shell介绍

shell介绍

shell是一个命令解释器,提供用户和机器之间的交互

  • 登录终端,显示的界面就是shell
  • 系统与硬件交互时是用的中间介质,它只是一个系统工具。

支持特定语法,好比逻辑判断,循环

每一个用户均可以有本身特定的shell

CentOS7默认shell为bash(Bourne Agin Shell)

  • sh的加强版本。

系统里还有其余的shell,叫zsh,ksh等


命令历史 history

  • 命令历史是保存在/root/.bash_history 里面的,能够cat进行查看。linux

  • 默认命令历史保存最大存1000条历史命令。能够echo $HISTSIZE进行查看数量。这个数量是环境变量控制的,能够更改。shell

[root@localhost ~]# echo $HISTSIZE
1000
  • 环境变量HISTSIZE 在/etc/profile中修改,可改为5000,改为多少保存多少。
vi /etc/profile
找到
HOSTNAME=`/usr/bin/hostname 2>/dev/null`
HISTSIZE=1000  ------------若是以为1000条不够能够改为5000-----------
if [ "$HISTCONTROL" = "ignorespace" ] ; then

更改以后必须退出一下终端从新进或者source /etc/profile 才会更改vim

[root@localhost ~]# echo $HISTSIZE
1000
[root@localhost ~]# source /etc/profile
[root@localhost ~]# echo $HISTSIZE
5000
  • 变量从新赋值

HISTTIMEFORMAT="%Y/%m/%d %H:%M:%S"bash

[root@localhost ~]# history 
    1  ls
    2  ls /etc/passwd
    3  cat /etc/passwd
    4  [root@localhost ~]# cat /etc/passwd
    5  root:x:0:0:root:/root:/bin/bash
    6  bin:x:1:1:bin:/bin:/sbin/nologin
    7  daemon:x:2:2:daemon:/sbin:/sbin/nologin
    8  adm:x:3:4:adm:/var/adm:/sbin/nologin
    9  lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin

从新赋值以后ssh

[root@localhost ~]# HISTTIMEFORMAT="%Y/%m/%d %H:%M:%S "
[root@localhost ~]# history 
    1  2017/08/30 19:20:18 ls
    2  2017/08/30 19:20:18 ls /etc/passwd
    3  2017/08/30 19:20:18 cat /etc/passwd
    4  2017/08/30 19:20:18 [root@localhost ~]# cat /etc/passwd
    5  2017/08/30 19:20:18 root:x:0:0:root:/root:/bin/bash
    6  2017/08/30 19:20:18 bin:x:1:1:bin:/bin:/sbin/nologin
    7  2017/08/30 19:20:18 daemon:x:2:2:daemon:/sbin:/sbin/nologin
    8  2017/08/30 19:20:18 adm:x:3:4:adm:/var/adm:/sbin/nologin

这时候再打开一个新的终端,这个环境变量是不生效的。工具

若是想让此环境变量全局生效,须要编辑 /etc/profile把HISTTIMEFORMAT="%Y/%m/%d %H:%M:%S "加到里面学习

fi

HOSTNAME=`/usr/bin/hostname 2>/dev/null`
HISTSIZE=5000
HISTTIMEFORMAT="%Y/%m/%d %H:%M:%S"
if [ "$HISTCONTROL" = "ignorespace" ] ; then
    export HISTCONTROL=ignoreboth

source /etc/profile后生效spa

  • 永久保存命令历史

chattr +a ~/.bash_history 设置了这个权限只能追加不能删除rest

  • 若是没有正常退出,历史命令保存不全

  • !!

表示运行最后一条历史命令code

  • !n

指定运行历史行数命令

  • !命令

表示命令历史里面最后一次输入这个命令的命令


命令补全及别名

  • tab键,敲一下不全命令,敲两下列出候选须要的命令

  • Centos7 支持参数补全,默认不能够,须要安装yum install -y bash-completion 以后重启下系统才能生效。

  • alias别名,给命令重新起个名字

[root@localhost ~]# alias restartnet='systemctl restart network.service'
[root@localhost ~]# restartnet
[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 restartnet='systemctl restart network.service'
alias rm='rm -i'
alias which='alias | /usr/bin/which --tty-only --read-alias --show-dot --show-tilde'
[root@localhost ~]#

alias 存在用户家目录下的 .bashrc文件里。

还有一些存在 /etc/profile.d/下面

unalias 取消自定义别名


通配符

*

[root@localhost ~]# ls
111  1eer  1.txt  2.txt  anaconda-ks.cfg  erwe
[root@localhost ~]# ls *.txt
1.txt  2.txt
[root@localhost ~]# 
[root@localhost ~]# ls 1*
1eer  1.txt

111:
[root@localhost ~]# ls *1*
1eer  1.txt

111:
[root@localhost ~]#

?

问号表示任意一个字符

[root@localhost ~]# ls ?.txt
1.txt  2.txt
[root@localhost ~]# ls *.txt
1.txt  2.txt  bbcc.txt  bb.txt
[root@localhost ~]#

[]

选择范围

[root@localhost ~]# ls [123].txt
1.txt  2.txt
[root@localhost ~]# ls [0-9].txt
1.txt  2.txt
[root@localhost ~]# ls [1].txt
1.txt
[root@localhost ~]# ls [2].txt
2.txt
[root@localhost ~]#

{}

也是选择范围,跟[]差很少,可是中间有逗号

[root@localhost ~]# ls {1,2}.txt
1.txt  2.txt
[root@localhost ~]#

输入输出重定向

>

把>前面的内容删除掉重定向到后面内容去。

>>

把>>前面的内容追加到后面内容去。

2>

表示把运行错误的信息定向到一个文件里

[root@localhost ~]# fddd
-bash: fddd: 未找到命令
[root@localhost ~]# fddd 2> 1.txt 
[root@localhost ~]# cat 1.txt 
-bash: fddd: 未找到命令

2>>

错误追加剧定向

&>

错误和正确都有的输出重定向

[root@localhost ~]# ls [12].txt asffsfs.txt &> 1.txt
[root@localhost ~]# cat 1.txt 
ls: 没法访问asffsfs.txt: 没有那个文件或目录
1.txt
2.txt
[root@localhost ~]#

<

输入重定向

把<右边的文件内容输入到<左边的命令里面去

[root@localhost ~]# wc -l < 1.txt
3
[root@localhost ~]#

管道符

  • “|”做用:把前面命令的输出结果交给后面的命令


做业控制

  • ctrl z 暂停一个任务

[root@localhost ~]# vim 1.txt 

[1]+  已中止               vim 1.txt
[root@localhost ~]# vim 2.txt 

[2]+  已中止               vim 2.txt
[root@localhost ~]# jobs
[1]-  已中止               vim 1.txt
[2]+  已中止               vim 2.txt
[root@localhost ~]#
  • jobs命令用于显示Linux中的任务列表及任务状态,包括后台运行的任务。

  • fg 恢复暂停的任务,能够后面跟数字参数,表示恢复第几个已暂停的任务

  • bg 后台运行任务,不停的输出信息,可是不耽误运行命令


shell变量

  • PATH,HOME,PWD,LOGNAME

  • 能够经过env 或set来查看变量,系统变量通常都是大些的英文字母

  • 变量名字的规则:字母,数字下划线,首位不能为数字

  • 变量有特殊符号时须要用单引号括起来

[root@localhost ~]# echo $a$bb
1
[root@localhost ~]# echo '$a$bb'
$a$bb
  • pstree命令会把linux系统中的全部进程以树形结构显示出来。若是没有该命令:yum install -y psmisc

[root@localhost ~]# pstree
systemd─┬─NetworkManager───2*[{NetworkManager}]
        ├─agetty
        ├─auditd───{auditd}
        ├─chronyd
        ├─crond
        ├─dbus-daemon───{dbus-daemon}
        ├─firewalld───{firewalld}
        ├─lvmetad
        ├─master─┬─pickup
        │        └─qmgr
        ├─polkitd───5*[{polkitd}]
        ├─rsyslogd───2*[{rsyslogd}]
        ├─sshd───sshd───bash─┬─pstree
        │                    └─2*[vim]
        ├─systemd-journal
        ├─systemd-logind
        ├─systemd-udevd
        ├─tuned───4*[{tuned}]
        └─vmtoolsd───{vmtoolsd}
[root@localhost ~]#
  • export 全局设置变量,在同一个sshd下的子shell等知道这个变量,向下生效。

  • unset 变量名字:取消变量


环境变量配置文件

  • 系统环境变量的配置文件

    • /etc/profile : 这个文件预设了几个重要的变量,例如PATH、USER、LOGNAME、MAIL、INPUTRC、HOSTNAME、HISTSIZE、umask等
    • /etc/bashrc : 这个文件主要预设umask以及PS1。这个PS1就是咱们在输入命令时前面的字符串。
  • 用户环境变量的配置文件

    • .bash_profile 该文件顶一个用户的我的化路径与环境变量的文件名称。每一个用户均可以使用该文件输入专属本身的shell信息,当用户登陆时,该文件仅仅执行一次。
    • .bashrc 该文件包含专属于本身的shell的bash信息,当登录或每次打开新的shell时,该文件被读取。
    • .bash_history 该文件用于记录命令历史。
    • .bash_logout 当退出shell时,会执行该文件,能够将一些须要清理的工做放到这个文件夹中。

扩展学习

相关文章
相关标签/搜索