Linux shell基础知识_8(上)


shell介绍_01

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

  • 支持特定语法,比如逻辑判读、循环

  • 每个用户都可以有自己特定的shell

  • CentOS7默认shell为bash(Bourne Agin Shell)

  • 还有zsh、ksh等

命令历史_02

  • history命令

[root@zyshanlinux-01 ~]# history  ##查看保存的命令

[root@zyshanlinux-01 ~]# history -c  ##只是清空内存里面的命令历史
[root@zyshanlinux-01 ~]# history
   11  history
[root@zyshanlinux-01 ~]# cat /root/.bash_history  ##存命令的文件里面的内容还是存在的
ls -l /bin
/bin/ls
/usr/bin/ls
cd /tmp/
##这些敲过的命令不是实时的存放到配置文件里的,只有当你退出当前终端后,才会存放到配置文件里
  • .bash_history

[root@zyshanlinux-01 ~]# ls /root/.bash_history  ##历史命令存放文件的路径
/root/.bash_history
[root@zyshanlinux-01 ~]# cat /root/.bash_history
  • 最大1000条

[root@zyshanlinux-01 ~]# history
    9  ls
   10  cd 009
   11  ls
   12  touch 004.txt
...
 1006  ls /root/.bash_history
 1007  cat /root/.bash_history
 1008  history
[root@zyshanlinux-01 ~]# 
  • 变量HISTSIZE

[root@zyshanlinux-01 ~]# echo $HISTSIZE
1000
[root@zyshanlinux-01 ~]# 
  • /etc/profile中修改

变量HISTXIZE在/etc/profile文件中定义

[root@zyshanlinux-01 ~]# vi /etc/profile

HOSTNAME=`/usr/bin/hostname 2>/dev/null`
HISTSIZE=2000                                        ##在这行把原来的1000改为2000
if [ "$HISTCONTROL" = "ignorespace" ] ; then
    export HISTCONTROL=ignoreboth

[root@zyshanlinux-01 ~]#
[root@zyshanlinux-01 ~]# echo $HISTSIZE  ##并不是把配置文件修改后,马上变的
1000
[root@zyshanlinux-01 ~]# source /etc/profile  ##需要输入这个命令或者重新进终端才可以生效
[root@zyshanlinux-01 ~]# echo $HISTSIZE
2000
[root@zyshanlinux-01 ~]# history
   11  history
   12  cat /root/.bash_history
   13  vi /etc/profile
   14  echo $HISTSIZE
   15  source /etc/profile
   16  echo $HISTSIZE
   17  history
  • HISTTIMEFORMAT="%Y/%m/%d %H:%M:%S"

[root@zyshanlinux-01 ~]# HISTTIMEFORMAT="%Y/%m/%d %H:%M:%S"  ##赋予历史命令格式,查看使用命令的时间
[root@zyshanlinux-01 ~]# echo $HISTTIMEFORMAT  ##确认是有格式,只是赋予当前终端的格式,第2个终端没有
%Y/%m/%d %H:%M:%S
[root@zyshanlinux-01 ~]# history
   11  2018/05/29 19:40:31history
   12  2018/05/29 19:41:16cat /root/.bash_history
   13  2018/05/29 19:49:11vi /etc/profile
   14  2018/05/29 19:52:03echo $HISTSIZE
   15  2018/05/29 19:52:13source /etc/profile
   16  2018/05/29 19:52:15echo $HISTSIZE
   17  2018/05/29 19:52:33history
   18  2018/05/29 19:57:14HISTTIMEFORMAT="%Y/%m/%d %H:%M:%S"
   19  2018/05/29 19:57:36echo $HISTTIMEFORMAT
   20  2018/05/29 19:57:42history
   
##如果想把这个格式永久保存,需要在配置文件里面保存设置
[root@zyshanlinux-01 ~]# vim /etc/profile

fi

HOSTNAME=`/usr/bin/hostname 2>/dev/null`
HISTSIZE=2000
HISTTIMEFORMAT="%Y/%m/%d %H:%M:%S"  ##在HISTSIZE=2000这行下面把格式添加上

[root@zyshanlinux-01 ~]# source !$  ##重启服务
source /etc/profile

[root@zyshanlinux-01 ~]# history  ##成功添加格式
    1  2018/05/29 20:09:54ls -l /bin
    2  2018/05/29 20:09:54/bin/ls
    3  2018/05/29 20:09:54/usr/bin/ls
  • 永久保存chattr +a ~/.bash_history

##即使设置了1000条限制,但a权限是只能追加,所以是不受这个1000条的限制了
[root@zyshanlinux-01 ~]# chattr +a ~/.bash_history  
[root@zyshanlinux-01 ~]# 
##非正常退出,保存的命令会部分丢失
  • !! ##前面最后一次输入的命令

[root@zyshanlinux-01 ~]# ls
002.txt  009  anaconda-ks.cfg
[root@zyshanlinux-01 ~]# !!
ls
002.txt  009  anaconda-ks.cfg
[root@zyshanlinux-01 ~]# !!
ls
002.txt  009  anaconda-ks.cfg
  • !n

 1004  2018/05/29 20:19:36ls
 1005  2018/05/29 20:19:53history
[root@zyshanlinux-01 ~]# !1004  ##指定历史命令的某一条命令
ls
002.txt  009  anaconda-ks.cfg
[root@zyshanlinux-01 ~]# 
  • !word

 1008  2018/05/29 20:24:32echo $?
 1009  2018/05/29 20:24:46ls
 1010  2018/05/29 20:24:51date
 1011  2018/05/29 20:24:58history
[root@zyshanlinux-01 ~]# !echo  ##从后往前数,最后一条带有echo的命令
echo $?
0

 1010  2018/05/29 20:24:51date
 1011  2018/05/29 20:24:58history
 1012  2018/05/29 20:25:14echo $?
 1013  2018/05/29 20:27:42history
[root@zyshanlinux-01 ~]# !date  ##叹号加上命令,它就从历史命令中从后往前历数相同的命令作为输入
date
2018年 05月 29日 星期二 20:28:02 CST

命令补全及别名_03

  • tab键,敲一下,敲两下 ##补全命令,存在异于别的命令字母开头敲一下补全,多个命令都是相同字母则需要敲两下

  • 参数补全,安装bash-completion

##centos6只是支持命令补全,centos7增加了参数补全
[root@zyshanlinux-01 ~]# yum install -y bash-completion  ##安装完,需要重启系统

[root@zyshanlinux-01 ~]# systemctl res  ##成功参数补全
rescue        reset-failed  restart       
[root@zyshanlinux-01 ~]# systemctl restart network
network-online.target  network.service                
[root@zyshanlinux-01 ~]# systemctl restart network.service 
  • alias别名给命令重新起个名字

[root@zyshanlinux-01 ~]# alias restartnet='systemctl restart network.service'
[root@zyshanlinux-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'
##取消别名
[root@zyshanlinux-01 profile.d]# unalias restartnet
[root@zyshanlinux-01 profile.d]# 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@zyshanlinux-01 profile.d]# 
  • 各用户都有自己配置别名的文件 ~/.bashrc

[root@zyshanlinux-01 ~]# vi .bashrc  ##只配置了部分

# .bashrc

# User specific aliases and functions

alias rm='rm -i'
alias cp='cp -i'
alias mv='mv -i'
  • ls /etc/profile.d/

[root@zyshanlinux-01 ~]# cd /etc/profile.d
[root@zyshanlinux-01 profile.d]# ls  ##其他的别名都分布在这些文件里面
256term.csh  bash_completion.sh  colorgrep.sh  colorls.sh  lang.sh   less.sh  vim.sh      which2.sh
256term.sh   colorgrep.csh       colorls.csh   lang.csh    less.csh  vim.csh  which2.csh
[root@zyshanlinux-01 profile.d]# 
  • 自定义的alias放到~/.bashrc

通配符_04

  • ls *.txt ##通配所有

[root@zyshanlinux-01 ~]# ls
002.txt  009  13.txt  1.txt  2.txt  3.txt  anaconda-ks.cfg
[root@zyshanlinux-01 ~]# ls *.txt
002.txt  13.txt  1.txt  2.txt  3.txt
[root@zyshanlinux-01 ~]# ls *txt
002.txt  13.txt  1.txt  2.txt  3.txt
[root@zyshanlinux-01 ~]# ls *txt*
002.txt  13.txt  1.txt  2.txt  3.txt
[root@zyshanlinux-01 ~]# 
  • ls ?.txt ##问号表示一个字符

[root@zyshanlinux-01 ~]# ls ?.txt
1.txt  2.txt  3.txt
[root@zyshanlinux-01 ~]# ls ?txt
ls: 无法访问?txt: 没有那个文件或目录
  • ls [0-9].txt

[root@zyshanlinux-01 ~]# ls [0-3].txt
1.txt  2.txt  3.txt
[root@zyshanlinux-01 ~]# ls [13].txt
1.txt  3.txt
[root@zyshanlinux-01 ~]# ls [0-13].txt
1.txt  3.txt
[root@zyshanlinux-01 ~]# ls
002.txt  009  13.txt  1.txt  2.txt  3.txt  anaconda-ks.cfg
[root@zyshanlinux-01 ~]# ls [0-14].txt
1.txt
[root@zyshanlinux-01 ~]# ls [1313].txt
1.txt  3.txt
[root@zyshanlinux-01 ~]# ls [0-3a-f].txt
1.txt  2.txt  3.txt  a.txt
  • ls {1,2}.txt

[root@zyshanlinux-01 ~]# ls
002.txt  009  13.txt  1.txt  2.txt  3.txt  anaconda-ks.cfg  a.txt  g.txt
[root@zyshanlinux-01 ~]# ls {1,13,g}.txt
13.txt  1.txt  g.txt

输入输出重定向_05

  • cat 1.txt > 2.txt ##把1.txt的内容重定向到2.txt,清除了2.txt的内容写入1.txt的内容(正确)

  • cat 1.txt >> 2.txt ##把1.txt的内容追加重定向到2.txt,在2.txt内容后面写入1.txt的内容(正确)

  • ls aaa.txt 2>err ##(错误)重定向

[root@zyshanlinux-01 ~]# lsaaa
-bash: lsaaa: 未找到命令
[root@zyshanlinux-01 ~]# lsaaa 2> a.txt
[root@zyshanlinux-01 ~]# cat a.txt
-bash: lsaaa: 未找到命令
[root@zyshanlinux-01 ~]# lsaaa 2>> a.txt
[root@zyshanlinux-01 ~]# cat a.txt
-bash: lsaaa: 未找到命令
-bash: lsaaa: 未找到命令
  • ls aaa.txt 2>>err ##(错误)追加重定向

​>   >>   2>  2>>              > + 2> == &>          ##正确与错误合在一起的符号
[root@zyshanlinux-01 ~]# ls [12].txt aaa.txt &>a.txt
[root@zyshanlinux-01 ~]# cat a.txt
ls: 无法访问aaa.txt: 没有那个文件或目录
1.txt
2.txt

正确与错误信息全都输入到1个文件

​[root@zyshanlinux-01 ~]# ls [12].txt aaa.txt &>>a.txt
[root@zyshanlinux-01 ~]# cat a.txt
ls: 无法访问aaa.txt: 没有那个文件或目录
1.txt
2.txt
ls: 无法访问aaa.txt: 没有那个文件或目录
1.txt
2.txt

将正确与错误的信息分别输入到不同的文件

​[root@zyshanlinux-01 ~]# ls [12].txt aaa.txt >1.txt 2>a.txt
[root@zyshanlinux-01 ~]# cat 1.txt
1.txt
2.txt
[root@zyshanlinux-01 ~]# cat a.txt
ls: 无法访问aaa.txt: 没有那个文件或目录
  • wc -l < 1.txt

[root@zyshanlinux-01 ~]# wc -l < 1.txt  ##查看1.txt的行号
0
[root@zyshanlinux-01 ~]# 2.txt < 1.txt  ##右边的内容只能重定向到左边的命令
-bash: 2.txt: 未找到命令
  • command >1.txt 2>&1

&1 表示标准正确输出目标。 因为前面已经定义了 >1.txt 标准正确输出到了1.txt,所以&1就是1.txt

command >1.txt 2>&1 就相当于将command的正确与错误信息都写在1.txt上

管道符和作业控制_06

  • cat 1.txt |wc -l ;cat 1.txt |grep 'aaa'

[root@zyshanlinux-01 ~]# find ./ -type f
./.bash_logout
./.bash_profile
./.bashrc
./.cshrc
./.tcshrc
./anaconda-ks.cfg
./.bash_history
./.ssh/known_hosts
./.ssh/.authorized_keys.swp
./.ssh/authorized_keys
./002.txt
./009/008/005.txt
./009/004.txt
./009/test01
./009/011.txt.bak
./.viminfo
./1.txt
./2.txt
./3.txt
./13.txt
./a.txt
./g.txt
./q.txt
[root@zyshanlinux-01 ~]# find ./ -type f |wc -l  ##把前面命令得出文件由管道符传递给后面同济文件个数
23
  • ctrl z 暂停一个任务

[root@zyshanlinux-01 ~]# vim 1.txt  ##进去编辑界面用ctrl z暂停

[1]+  已停止               vim 1.txt
[root@zyshanlinux-01 ~]# fg  ##用fg回到编辑界面
vim 1.txt

[1]+  已停止               vim 1.txt
[root@zyshanlinux-01 ~]# vim 002.txt  ##同时打开第二个编辑界界面,所以就有[2]+  已停止

[2]+  已停止               vim 002.tx
  • jobs查看后台的任务

[root@zyshanlinux-01 ~]# jobs
[1]-  已停止               vim 1.txt
[2]+  已停止               vim 002.txt
  • bg[id]把任务调到后台

[root@zyshanlinux-01 ~]# sleep 1000
^Z
[1]+  已停止               sleep 1000
[root@zyshanlinux-01 ~]# sleep 500
^Z
[2]+  已停止               sleep 500
[root@zyshanlinux-01 ~]# jobs
[1]-  已停止               sleep 1000
[2]+  已停止               sleep 500
[root@zyshanlinux-01 ~]# bg 2
[2]+ sleep 500 &
[root@zyshanlinux-01 ~]# jobs
[1]+  已停止               sleep 1000
[2]-  运行中               sleep 500 &
[root@zyshanlinux-01 ~]# bg 1
[1]+ sleep 1000 &
[root@zyshanlinux-01 ~]# jobs
[1]-  运行中               sleep 1000 &
[2]+  运行中               sleep 500 &
  • fg[id]把任务调到前台

[root@zyshanlinux-01 ~]# fg 1
sleep 1000
^Z
[1]+  已停止               sleep 1000
[root@zyshanlinux-01 ~]# jobs
[1]+  已停止               sleep 1000
[2]-  运行中               sleep 500 &

前台运行不能操作命令界面,后台运行可以操作命令界面

  • 命令后面加&直接丢到后台

[root@zyshanlinux-01 ~]# jobs
[root@zyshanlinux-01 ~]# sleep 1000 &
[1] 1443
[root@zyshanlinux-01 ~]# jobs
[1]+  运行中               sleep 1000 &

新打开一个终端用jobs是看不到前一个终端运行的sleep 1000的,新终端可以用ps aux |grep sleep查看进程

[root@zyshanlinux-01 ~]# jobs
[root@zyshanlinux-01 ~]# ps aux |grep sleep
root      1443  0.0  0.0 107948   352 pts/0    S    22:38   0:00 sleep 1000
root      1468  0.0  0.0 112720   972 pts/1    S+   22:39   0:00 grep --color=auto sleep
[root@zyshanlinux-01 ~]# 

shell变量_7&8

  • PATH(系统内置变量) , HOME , PWD , LOGNAME

  • env命令

[root@zyshanlinux-01 ~]# env  ##获取变量,查看系统常用的环境变量
XDG_SESSION_ID=1
HOSTNAME=zyshanlinux-01
SELINUX_ROLE_REQUESTED=
TERM=xterm
SHELL=/bin/bash
HISTSIZE=2000
SSH_CLIENT=192.168.106.1 5469 22
SELINUX_USE_CURRENT_RANGE=
SSH_TTY=/dev/pts/0
USER=root
LS_COLORS=rs=0:di=01;34:ln=01;36:mh=00:pi=40;33:so=01;35:do=01;35:bd=40;33;01:cd=40;33;01:or=40;31;01:mi=01;05;37;41:su=37;41:sg=30;43:ca=30;41:tw=30;42:ow=34;42:st=37;44:ex=01;32:*.tar=01;31:*.tgz=01;31:*.arc=01;31:*.arj=01;31:*.taz=01;31:*.lha=01;31:*.lz4=01;31:*.lzh=01;31:*.lzma=01;31:*.tlz=01;31:*.txz=01;31:*.tzo=01;31:*.t7z=01;31:*.zip=01;31:*.z=01;31:*.Z=01;31:*.dz=01;31:*.gz=01;31:*.lrz=01;31:*.lz=01;31:*.lzo=01;31:*.xz=01;31:*.bz2=01;31:*.bz=01;31:*.tbz=01;31:*.tbz2=01;31:*.tz=01;31:*.deb=01;31:*.rpm=01;31:*.jar=01;31:*.war=01;31:*.ear=01;31:*.sar=01;31:*.rar=01;31:*.alz=01;31:*.ace=01;31:*.zoo=01;31:*.cpio=01;31:*.7z=01;31:*.rz=01;31:*.cab=01;31:*.jpg=01;35:*.jpeg=01;35:*.gif=01;35:*.bmp=01;35:*.pbm=01;35:*.pgm=01;35:*.ppm=01;35:*.tga=01;35:*.xbm=01;35:*.xpm=01;35:*.tif=01;35:*.tiff=01;35:*.png=01;35:*.svg=01;35:*.svgz=01;35:*.mng=01;35:*.pcx=01;35:*.mov=01;35:*.mpg=01;35:*.mpeg=01;35:*.m2v=01;35:*.mkv=01;35:*.webm=01;35:*.ogm=01;35:*.mp4=01;35:*.m4v=01;35:*.mp4v=01;35:*.vob=01;35:*.qt=01;35:*.nuv=01;35:*.wmv=01;35:*.asf=01;35:*.rm=01;35:*.rmvb=01;35:*.flc=01;35:*.avi=01;35:*.fli=01;35:*.flv=01;35:*.gl=01;35:*.dl=01;35:*.xcf=01;35:*.xwd=01;35:*.yuv=01;35:*.cgm=01;35:*.emf=01;35:*.axv=01;35:*.anx=01;35:*.ogv=01;35:*.ogx=01;35:*.aac=01;36:*.au=01;36:*.flac=01;36:*.mid=01;36:*.midi=01;36:*.mka=01;36:*.mp3=01;36:*.mpc=01;36:*.ogg=01;36:*.ra=01;36:*.wav=01;36:*.axa=01;36:*.oga=01;36:*.spx=01;36:*.xspf=01;36:
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.106.1 5469 192.168.106.128 22
LESSOPEN=||/usr/bin/lesspipe.sh %s
XDG_RUNTIME_DIR=/run/user/0
_=/usr/bin/env
[root@zyshanlinux-01 ~]# 


  • set命令多了很多变量,并且包括用户自定义的变量

[root@zyshanlinux-01 ~]# set


  • 自当义变量a=1

[root@zyshanlinux-01 ~]# echo $PATH
/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/bin
[root@zyshanlinux-01 ~]# a=111
[root@zyshanlinux-01 ~]# echo $a
111
[root@zyshanlinux-01 ~]# set |grep 111
_=111
a=111
[root@zyshanlinux-01 ~]# set |less  ##可以查到自定义的变量,用/搜索


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

[root@zyshanlinux-01 ~]# a1=2
[root@zyshanlinux-01 ~]# a_1=8
[root@zyshanlinux-01 ~]# _a1=2
[root@zyshanlinux-01 ~]# 1_a=9
-bash: 1_a=9: 未找到命令
[root@zyshanlinux-01 ~]# echo $a1
2
[root@zyshanlinux-01 ~]# echo $a_1
8
[root@zyshanlinux-01 ~]# echo $_a1
2


  • 变量值由特殊符号时需要用单引号括起来

[root@zyshanlinux-01 ~]# a="a$bc"
[root@zyshanlinux-01 ~]# echo $a
a
[root@zyshanlinux-01 ~]# a='a$bc'
[root@zyshanlinux-01 ~]# echo $a
a$bc


  • 变量的累加

[root@zyshanlinux-01 ~]# a=1
[root@zyshanlinux-01 ~]# b=2
[root@zyshanlinux-01 ~]# echo $a$b
12
[root@zyshanlinux-01 ~]# a='a$bc'
[root@zyshanlinux-01 ~]# echo $a$b
a$bc2
[root@zyshanlinux-01 ~]# c="a$v"c
[root@zyshanlinux-01 ~]# echo $c
ac
[root@zyshanlinux-01 ~]# c="a$b"c
[root@zyshanlinux-01 ~]# echo $c
a2c
[root@zyshanlinux-01 ~]# c='a$b'c
[root@zyshanlinux-01 ~]# echo $c
a$bc
[root@zyshanlinux-01 ~]# c=a"$b"c
[root@zyshanlinux-01 ~]# echo $c
a2c


  • 全局变量export b=2

[root@zyshanlinux-01 ~]# zyshan=linux
[root@zyshanlinux-01 ~]# echo $zyshan
linux
[root@zyshanlinux-01 ~]# bash
[root@zyshanlinux-01 ~]# w
 19:00:18 up 38 min,  2 users,  load average: 0.00, 0.01, 0.05
USER     TTY      FROM             LOGIN@   IDLE   JCPU   PCPU WHAT
root     pts/0    192.168.106.1    18:22    2.00s  0.15s  0.00s w
root     pts/1    192.168.106.1    18:54    4:50   0.01s  0.01s -bash
[root@zyshanlinux-01 ~]# pstree
systemd─┬─NetworkManager───2*[{NetworkManager}]
        ├─VGAuthService
        ├─agetty
        ├─auditd───{auditd}
        ├─chronyd
        ├─crond
        ├─dbus-daemon───{dbus-daemon}
        ├─firewalld───{firewalld}
        ├─lvmetad
        ├─master─┬─pickup
        │        └─qmgr
        ├─polkitd───5*[{polkitd}]
        ├─rsyslogd───2*[{rsyslogd}]
        ├─sshd───sshd─┬─bash───bash───pstree  ##我们现在所处的位置
        │             └─bash
        ├─systemd-journal
        ├─systemd-logind
        ├─systemd-udevd
        ├─tuned───4*[{tuned}]
        └─vmtoolsd───{vmtoolsd}
[root@zyshanlinux-01 ~]# echo $zyshan  ##非本地没有变量,非全局变量

[root@zyshanlinux-01 ~]# exit  ##退出子bash
exit
[root@zyshanlinux-01 ~]# echo $zyshan  ##又看到变量了,来到了本地
linux
[root@zyshanlinux-01 ~]# pstree
systemd─┬─NetworkManager───2*[{NetworkManager}]
        ├─VGAuthService
        ├─agetty
        ├─anacron
        ├─auditd───{auditd}
        ├─chronyd
        ├─crond
        ├─dbus-daemon───{dbus-daemon}
        ├─firewalld───{firewalld}
        ├─lvmetad
        ├─master─┬─pickup
        │        └─qmgr
        ├─polkitd───5*[{polkitd}]
        ├─rsyslogd───2*[{rsyslogd}]
        ├─sshd───sshd─┬─bash───pstree  ##子bash退出了
        │             └─bash
        ├─systemd-journal
        ├─systemd-logind
        ├─systemd-udevd
        ├─tuned───4*[{tuned}]
        └─vmtoolsd───{vmtoolsd}


[root@zyshanlinux-01 ~]# export zyshan=linux
[root@zyshanlinux-01 ~]# echo $zyshan  ##全局变量
linux
[root@zyshanlinux-01 ~]# bash
[root@zyshanlinux-01 ~]# echo $zyshan  ##全局变量,来到子bash也可以查看到
linux
[root@zyshanlinux-01 ~]# bash
[root@zyshanlinux-01 ~]# echo $zyshan  ##全局环境变量,来到孙bash也可以查看到0.0
linux
[root@zyshanlinux-01 ~]# pstree
systemd─┬─NetworkManager───2*[{NetworkManager}]
        ├─VGAuthService
        ├─agetty
        ├─anacron
        ├─auditd───{auditd}
        ├─chronyd
        ├─crond
        ├─dbus-daemon───{dbus-daemon}
        ├─firewalld───{firewalld}
        ├─lvmetad
        ├─master─┬─pickup
        │        └─qmgr
        ├─polkitd───5*[{polkitd}]
        ├─rsyslogd───2*[{rsyslogd}]
        ├─sshd───sshd───bash───bash───bash───pstree
        ├─systemd-journal
        ├─systemd-logind
        ├─systemd-udevd
        ├─tuned───4*[{tuned}]
        └─vmtoolsd───{vmtoolsd}


不同终端的bash路线

        ├─sshd───sshd───bash───bash───bash───pstree
        ├─systemd-journal
        ├─systemd-logind
        ├─systemd-udevd
        ├─tuned───4*[{tuned}]
        └─vmtoolsd───{vmtoolsd}
[root@zyshanlinux-01 ~]# echo $zyshan
linux
[root@zyshanlinux-01 ~]# export b=123
[root@zyshanlinux-01 ~]# echo $b
123
[root@zyshanlinux-01 ~]# exit  ##退出孙bash
exit
[root@zyshanlinux-01 ~]# echo $b  ##孙bash自定义的全局变量在子bash不存在,说明这个全局变量向下不向上

[root@zyshanlinux-01 ~]# 


  • unset变量

[root@zyshanlinux-01 ~]# echo $zyshan
linux
[root@zyshanlinux-01 ~]# unset zyshan  ##取消当前bash的全局变量
[root@zyshanlinux-01 ~]# echo $zyshan



环境变量配置文件_9

  • /etc/profile 用户环境变量,交互,登录才执行

  • /etc/bashrc用户不用登录,执行shell就生效

  • ~/.bashrc

  • ~/.bash_profile

  • ~/.bash_history

  • ~/.bash_logout ##用户登出命令配置,可以在这里加上登出时删除用户的命令历史,就可以加到该文件里面

  • PS1='\[\033[01;32m\]\u@\h\[\033[00m\]:\[\033[01;36m\]\w\[\033[00m\]\$'

source .bash_profile和. .bash_profile是同一个意思,source=. 都是加载一些配置文件的。

[root@zyshanlinux-01 ~]# PS1='[\u@\h \w]\$'  ##W显示绝对路径最后一个目录,w显示绝对路径
[root@zyshanlinux-01 ~]# PS1='[\u@\h \w]\$'
[root@zyshanlinux-01 ~]#cd 009
[root@zyshanlinux-01 ~/009]#cd 010
[root@zyshanlinux-01 ~/009/010]#PS1='\u@\h \w\$'  ##去掉中括号
root@zyshanlinux-01 ~/009/010#PS1='<\u@\h \w>\$'  ##加上尖括号
<root@zyshanlinux-01 ~/009/010>#
<root@zyshanlinux-01 ~/009/010>#PS1='\[\033[01;32m\]\u@\h\[\033[00m\]:\[\033[01;36m\]\w\[\033[00m\]\$'  ##变色命令
root@zyshanlinux-01:~/009/010#
root@zyshanlinux-01:~/009/010#


扩展阅读:

http://ask.apelearn.com/question/7719

  • 1
    点赞
  • 17
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值