shell介绍

shell介绍

shell是一个命令解释器,提供用户和机器之间的交互
  • 登陆终端,显示的界面就是shell
  • 系统与硬件交互时是用的中间介质,它只是一个系统工具。
支持特定语法,比如逻辑判断,循环
每个用户都可以有自己特定的shell
CentOS7默认shell为bash(Bourne Agin Shell)
  • sh的增强版本。
系统里还有其他的shell,叫zsh,ksh等

命令历史 history

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

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

[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 才会更改

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

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

[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

重新赋值之后

[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后生效

  • 永久保存命令历史

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

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

  • !!

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

  • !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时,会执行该文件,可以将一些需要清理的工作放到这个文件夹中。

扩展学习

转载于:https://my.oschina.net/u/3852961/blog/1822393

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值