标签:8.6 管道符和作业控制 8.7/8.8 shell变量 8.9 环境变量配置文件
8.6 管道符和作业控制
8.7/8.8 shell变量
8.9 环境变量配置文件
扩展
bashrc和bash_profile的区别 http://ask.apelearn.com/question/7719
# 8.6 管道符和作业控制

- 管道符的命令就是把前面输出的结果交给后面的命令
```
[root@aminglinux-01 ~]# ls
111 1_heard.txt 1.txt 1.txt.bak 2.txt 3.txt anaconda-ks.cfg.1 bb.txt
123 1_sorft.txt 1.txt~ 234 2.txt.bak 4.txt a.txt biji.txt
[root@aminglinux-01 ~]# ls |wc -l wc -l 是查询有多少文件
16
[root@aminglinux-01 ~]# find ./ -type f
./.bash_logout
./.bash_profile
./.bashrc
./.cshrc
./.tcshrc
./.bash_history
./.ssh/known_hosts
./.ssh/authorized_keys
./.ssh/id_rsa
./.ssh/id_rsa.pub
./anaconda-ks.cfg.1
./.lesshst
./234/aminglinux
./234/aminglinux111
./3.txt
./1.txt~
./111/12.txt
./111/.12.txt.swp
./111/.12.txt.swx
./111/12_txt.swp
./111/4913
./111/12.tx~
./1_heard.txt
./2.txt.bak
./1.txt.bak
./4.txt
./biji.txt
./.gitconfig
./.viminfo
./2.txt
./1.txt
./a.txt
./bb.txt
[root@aminglinux-01 ~]# find ./ -type f |wc -l
33
[root@aminglinux-01 ~]#
```
- ctrl + z 可以暂停一个任务,
- fg 恢复这个任务,调到前台来 foreground,运行多个任务可以使用fg 1 fg 2
- bg 使它到后台运行 background 运行多个任务 使用bg 1 bg 2,如果不加参数 就是默认的最后一个运行的程序
- jobs 可以把你停止的任务给列出来
```
[root@aminglinux-01 ~]# vim 1.txt
[1]+ 已停止 vim 1.txt
[root@aminglinux-01 ~]# df -h
文件系统 容量 已用 可用 已用% 挂载点
/dev/sda3 28G 1.5G 27G 6% /
devtmpfs 479M 0 479M 0% /dev
tmpfs 489M 0 489M 0% /dev/shm
tmpfs 489M 6.7M 482M 2% /run
tmpfs 489M 0 489M 0% /sys/fs/cgroup
/dev/sda1 197M 109M 88M 56% /boot
tmpfs 98M 0 98M 0% /run/user/0
[root@aminglinux-01 ~]# cat 1.txt
1.txt
2.txt
[root@aminglinux-01 ~]# fg
vim 1.txt
[1]+ 已停止 vim 1.txt
[root@aminglinux-01 ~]# vim aa.txt
[2]+ 已停止 vim aa.txt
[root@aminglinux-01 ~]# jobs
[1]- 已停止 vim 1.txt
[2]+ 已停止 vim aa.txt
[root@aminglinux-01 ~]# fg 2
vim aa.txt
[2]+ 已停止 vim aa.txt
```
- bg 在后台运行 bg 1 bg 2 加上参数 不加参数默认是最后一个运行的程序
```
[root@aminglinux-01 ~]# bg 2
[2]+ vim aa.txt &
[root@aminglinux-01 ~]# jobs
[1]- 已停止 vim 1.txt
[2]+ 已停止 vim aa.txt
```
- fg 在前面运行 fg 1 fg 2 加上参数 不加参数默认是最后一个运行的程序
```
[root@aminglinux-01 ~]# sleep 1000
^Z
[1]+ 已停止 sleep 1000
[root@aminglinux-01 ~]# jobs
[1]+ 已停止 sleep 1000
[root@aminglinux-01 ~]# sleep 200
bg
^C
[root@aminglinux-01 ~]# fg
sleep 1000
^C
[root@aminglinux-01 ~]# jobs
[root@aminglinux-01 ~]# sleep 1000
^Z
[1]+ 已停止 sleep 1000
[root@aminglinux-01 ~]# jobs
[1]+ 已停止 sleep 1000
[root@aminglinux-01 ~]# sleep 200
^Z
[2]+ 已停止 sleep 200
[root@aminglinux-01 ~]# jobs
[1]- 已停止 sleep 1000
[2]+ 已停止 sleep 200
[root@aminglinux-01 ~]# fg
sleep 200
^Z
[2]+ 已停止 sleep 200
[root@aminglinux-01 ~]# bg 1
[1]- sleep 1000 &
[root@aminglinux-01 ~]# jobs
[1]- 运行中 sleep 1000 &
[2]+ 已停止 sleep 200
[root@aminglinux-01 ~]# fg 1
sleep 1000
^C
[root@aminglinux-01 ~]# jobs
[2]+ 已停止 sleep 200
[root@aminglinux-01 ~]# fg
sleep 200
^C
[root@aminglinux-01 ~]# sleep 100&
[1] 2282
[root@aminglinux-01 ~]# jobs
[1]+ 运行中 sleep 100 &
[root@aminglinux-01 ~]# sleep 100 &
[2] 2283
[root@aminglinux-01 ~]# jobs
[1]- 运行中 sleep 100 &
[2]+ 运行中 sleep 100 &
[root@aminglinux-01 ~]# fg bg ctrl z &
[3] 2307
[1] 完成 sleep 100
[2] 完成 sleep 100
[root@aminglinux-01 ~]# -bash: fg: 无任务控制
^C
[3]+ 退出 1 fg bg ctrl z
[root@aminglinux-01 ~]# fg bg ctrl z &^C
[root@aminglinux-01 ~]#
```
# 8.7/8.8 shell 变量

- 命令env 可以查看系统常用的变量
```
[root@aminglinux-01 ~]# env
XDG_SESSION_ID=3
HOSTNAME=aminglinux-01
SELINUX_ROLE_REQUESTED=
TERM=xterm
SHELL=/bin/bash
HISTSIZE=5000
SSH_CLIENT=192.168.202.1 51656 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.202.1 51656 192.168.202.130 22
LESSOPEN=||/usr/bin/lesspipe.sh %s
XDG_RUNTIME_DIR=/run/user/0
_=/usr/bin/env
[root@aminglinux-01 ~]#
```
- 命令set 不仅可以查看系统的变量 也可以查看用户自定义的一些变量
- 不仅有系统的变量 也有用户自定义的变量
```
[root@aminglinux-01 ~]# echo $PATH
/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/bin
[root@aminglinux-01 ~]# a=111
[root@aminglinux-01 ~]# echo $a
111
[root@aminglinux-01 ~]# set |grep 111
_=111
a=111
[root@aminglinux-01 ~]#
```
可以在set 里面找到 自定义变量 env里找不到,因为env里都是系统的,set既有系统的也有用户自定义的
```
[root@aminglinux-01 ~]# set |less
...skipping...
a=111
colors=/root/.dircolors
__expand_tilde_by_ref ()
{
if [[ ${!1} == \~* ]]; then
if [[ ${!1} == */* ]]; then
eval $1="${!1/%\/*}"/‘${!1#*/}‘;
else
eval $1="${!1}";
fi;
fi
}
__get_cword_at_cursor_by_ref ()
{
local cword words=();
__reassemble_comp_words_by_ref "$1" words cword;
local i cur index=$COMP_POINT lead=${COMP_LINE:0:$COMP_POINT};
if [[ $index -gt 0 && ( -n $lead && -n ${lead//[[:space:]]} ) ]]; then
cur=$COMP_LINE;
for ((i = 0; i <= cword; ++i ))
do
while [[ ${#cur} -ge ${#words[i]} && "${cur:0:${#words[i]}}" != "${words[i]}" ]
/a=1
命令env查看变量
[root@aminglinux-01 ~]# env
XDG_SESSION_ID=3
HOSTNAME=aminglinux-01
SELINUX_ROLE_REQUESTED=
TERM=xterm
SHELL=/bin/bash
HISTSIZE=5000
SSH_CLIENT=192.168.202.1 51656 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.202.1 51656 192.168.202.130 22
LESSOPEN=||/usr/bin/lesspipe.sh %s
XDG_RUNTIME_DIR=/run/user/0
_=/usr/bin/env
[root@aminglinux-01 ~]#
```
- [ ] 变量的规则:字母、数字、下划线,首位不能时数字
```
[root@aminglinux-01 ~]#
[root@aminglinux-01 ~]# a1=2
[root@aminglinux-01 ~]# echo $a1
2
[root@aminglinux-01 ~]# a_1=3
[root@aminglinux-01 ~]# echo $a_1
3
[root@aminglinux-01 ~]# _a1=4
[root@aminglinux-01 ~]# echo $_a1
4
[root@aminglinux-01 ~]# 1aa=2
-bash: 1aa=2: 未找到命令
[root@aminglinux-01 ~]# 2aa=3
-bash: 2aa=3: 未找到命令
[root@aminglinux-01 ~]#
```
```
[root@aminglinux-01 ~]# a=a b c
-bash: b: 未找到命令
[root@aminglinux-01 ~]# a=‘a b c‘
[root@aminglinux-01 ~]# echo $a
a b c
[root@aminglinux-01 ~]# a="a b c"
[root@aminglinux-01 ~]# echo $a
a b c
[root@aminglinux-01 ~]#
最好使用单引号
[root@aminglinux-01 ~]# a="a$bc"
[root@aminglinux-01 ~]# echo $a
a
[root@aminglinux-01 ~]# a=‘a$bc‘
[root@aminglinux-01 ~]# echo $a
a$bc
[root@aminglinux-01 ~]#
```
- 变量叠加
```
[root@aminglinux-01 ~]# a=1
[root@aminglinux-01 ~]# b=2
[root@aminglinux-01 ~]# echo $a$b
12
[root@aminglinux-01 ~]# a=‘a$bc‘
[root@aminglinux-01 ~]# echo $a$b
a$bc2
[root@aminglinux-01 ~]# c="a$bc"
[root@aminglinux-01 ~]# echo $c
a
[root@aminglinux-01 ~]# c="a$b"c
[root@aminglinux-01 ~]# echo $c
a2c
[root@aminglinux-01 ~]# c=‘a$b‘c
[root@aminglinux-01 ~]# echo $c
a$bc
[root@aminglinux-01 ~]# c=a"$b"c
[root@aminglinux-01 ~]# echo $c
a2c
[root@aminglinux-01 ~]#
```
- [ ] 全局变量export b=2
- 用w查看当前登录系统的用户
- 终端1
```
[root@aminglinux-01 ~]# w
23:36:46 up 1:45, 3 users, load average: 0.00, 0.01, 0.05
USER TTY FROM LOGIN@ IDLE JCPU PCPU WHAT
root tty1 22:12 1:24m 0.05s 0.05s -bash
root pts/0 192.168.202.1 22:12 6.00s 0.19s 0.03s w
root pts/1 192.168.202.1 23:36 11.00s 0.04s 0.04s -bash
[root@aminglinux-01 ~]#
[root@aminglinux-01 ~]# echo $SSH_TTY
/dev/pts/0
[root@aminglinux-01 ~]#
[root@aminglinux-01 ~]# echo $SSH_TTY
/dev/pts/0
[root@aminglinux-01 ~]# aming=linux
[root@aminglinux-01 ~]# echo $aming
linux
[root@aminglinux-01 ~]#
```
- echo $SSH_TTY 这个变量可以显示当前在哪个下 可以看到上面命令 显示当前在pts/0下

- 终端2
```
[root@aminglinux-01 ~]# echo $SSH_TTY
/dev/pts/1
[root@aminglinux-01 ~]# echo $aming
[root@aminglinux-01 ~]#
```
- 终端1
```
[root@aminglinux-01 ~]# w
23:36:46 up 1:45, 3 users, load average: 0.00, 0.01, 0.05
USER TTY FROM LOGIN@ IDLE JCPU PCPU WHAT
root tty1 22:12 1:24m 0.05s 0.05s -bash
root pts/0 192.168.202.1 22:12 6.00s 0.19s 0.03s w
root pts/1 192.168.202.1 23:36 11.00s 0.04s 0.04s -bash
[root@aminglinux-01 ~]# echo $SSH_TTY
/dev/pts/0
[root@aminglinux-01 ~]# aming=linux
[root@aminglinux-01 ~]# echo $aming
linux
[root@aminglinux-01 ~]# bash
[root@aminglinux-01 ~]# w
23:43:29 up 1:52, 3 users, load average: 0.00, 0.01, 0.05
USER TTY FROM LOGIN@ IDLE JCPU PCPU WHAT
root tty1 22:12 1:30m 0.05s 0.05s -bash
root pts/0 192.168.202.1 22:12 1.00s 0.20s 0.00s w
root pts/1 192.168.202.1 23:36 41.00s 0.04s 0.04s -bash
[root@aminglinux-01 ~]# pstree
systemd─┬─NetworkManager───2*[{NetworkManager}]
├─auditd───{auditd}
├─chronyd
├─crond
├─dbus-daemon───{dbus-daemon}
├─firewalld───{firewalld}
├─login───bash
├─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@aminglinux-01 ~]# echo $aming
[root@aminglinux-01 ~]# exit
exit
[root@aminglinux-01 ~]# pstree
systemd─┬─NetworkManager───2*[{NetworkManager}]
├─auditd───{auditd}
├─chronyd
├─crond
├─dbus-daemon───{dbus-daemon}
├─firewalld───{firewalld}
├─login───bash
├─lvmetad
├─master─┬─pickup
│ └─qmgr
├─polkitd───5*[{polkitd}]
├─rsyslogd───2*[{rsyslogd}]
├─sshd───sshd─┬─bash───pstree
│ └─bash
├─systemd-journal
├─systemd-logind
├─systemd-udevd
├─tuned───4*[{tuned}]
└─vmtoolsd───{vmtoolsd}
[root@aminglinux-01 ~]# echo $aming
linux
[root@aminglinux-01 ~]# export $aming
[root@aminglinux-01 ~]# bash
[root@aminglinux-01 ~]# echo $aming
[root@aminglinux-01 ~]# exit
exit
[root@aminglinux-01 ~]# export aming=linux
[root@aminglinux-01 ~]# echo $aming
linux
[root@aminglinux-01 ~]# bash
[root@aminglinux-01 ~]# echo $aming
linux
[root@aminglinux-01 ~]# bash
[root@aminglinux-01 ~]# pstree
systemd─┬─NetworkManager───2*[{NetworkManager}]
├─auditd───{auditd}
├─chronyd
├─crond
├─dbus-daemon───{dbus-daemon}
├─firewalld───{firewalld}
├─login───bash
├─lvmetad
├─master─┬─pickup
│ └─qmgr
├─polkitd───5*[{polkitd}]
├─rsyslogd───2*[{rsyslogd}]
├─sshd───sshd─┬─bash───bash───bash───pstree
│ └─bash
├─systemd-journal
├─systemd-logind
├─systemd-udevd
├─tuned───4*[{tuned}]
└─vmtoolsd───{vmtoolsd}
[root@aminglinux-01 ~]# echo $aming
linux
[root@aminglinux-01 ~]#
```
- 终端2
```
[root@aminglinux-01 ~]# echo $SSH_TTY
/dev/pts/1
[root@aminglinux-01 ~]# echo $aming
[root@aminglinux-01 ~]# pstree
systemd─┬─NetworkManager───2*[{NetworkManager}]
├─auditd───{auditd}
├─chronyd
├─crond
├─dbus-daemon───{dbus-daemon}
├─firewalld───{firewalld}
├─login───bash
├─lvmetad
├─master─┬─pickup
│ └─qmgr
├─polkitd───5*[{polkitd}]
├─rsyslogd───2*[{rsyslogd}]
├─sshd───sshd─┬─bash───bash───bash
│ └─bash───pstree
├─systemd-journal
├─systemd-logind
├─systemd-udevd
├─tuned───4*[{tuned}]
└─vmtoolsd───{vmtoolsd}
[root@aminglinux-01 ~]#
```
- 重新打开一个终端
```
[root@aminglinux-01 ~]# pstree
systemd─┬─NetworkManager───2*[{NetworkManager}]
├─auditd───{auditd}
├─chronyd
├─crond
├─dbus-daemon───{dbus-daemon}
├─firewalld───{firewalld}
├─login───bash
├─lvmetad
├─master─┬─pickup
│ └─qmgr
├─polkitd───5*[{polkitd}]
├─rsyslogd───2*[{rsyslogd}]
├─sshd─┬─sshd───bash───bash───bash
│ └─sshd───bash───pstree
├─systemd-journal
├─systemd-logind
├─systemd-udevd
├─tuned───4*[{tuned}]
└─vmtoolsd───{vmtoolsd}
[root@aminglinux-01 ~]# echo $aming
[root@aminglinux-01 ~]#
```
- 这个export变量仅仅是针对 子shell 才可以
- 回到终端1
```
[root@aminglinux-01 ~]# pstree
systemd─┬─NetworkManager───2*[{NetworkManager}]
├─auditd───{auditd}
├─chronyd
├─crond
├─dbus-daemon───{dbus-daemon}
├─firewalld───{firewalld}
├─login───bash
├─lvmetad
├─master─┬─pickup
│ └─qmgr
├─polkitd───5*[{polkitd}]
├─rsyslogd───2*[{rsyslogd}]
├─sshd─┬─sshd───bash───bash───bash───pstree
│ └─sshd───bash
├─systemd-journal
├─systemd-logind
├─systemd-udevd
├─tuned───4*[{tuned}]
└─vmtoolsd───{vmtoolsd}
[root@aminglinux-01 ~]# export b=123
[root@aminglinux-01 ~]# echo $b
123
[root@aminglinux-01 ~]# exit
exit
[root@aminglinux-01 ~]# echo $b
[root@aminglinux-01 ~]#
```
- export 变量所谓的全局变量指的是 向下继承 (子shell ,孙shell 子子shell),不会向上继承(父shell)
- 取消变量 unset 后面跟变量的名字
```
[root@aminglinux-01 ~]# echo $aming
linux
[root@aminglinux-01 ~]# unset aming
[root@aminglinux-01 ~]# echo $aming
[root@aminglinux-01 ~]#
```
- export 变量格式 export c=123 , 打开子shell 直接运行bash 就可以了
```
[root@aminglinux-01 ~]# export c=123
[root@aminglinux-01 ~]#
```
# 8.9 环境变量配置文件
- 系统层次
1. /etc/profile 用户环境变量,交互,登录才执行
2. /etc/bashrc 用户不用登录,执行shell就生效
- profile和bashrc这俩个个类型,关于系统的这俩个文件不要动,平时不要去编辑它,当遇到一些需求的时候,需要编辑配置文件的时候,可以编辑用户家目录下的,也就是用户自己的。
-比如你编辑.bash_profile
```
[root@aminglinux-01 ~]# vim .bash_profile
# .bash_profile
# Get the aliases and functions
if [ -f ~/.bashrc ]; then
. ~/.bashrc 这里的.相当于之前的source 命令
fi
# User specific environment and startup programs
PATH=$PATH:$HOME/bin
export PATH
~
~
".bash_profile" 12L, 176C 1,1 全部
[root@aminglinux-01 ~]# source .bash_profile^C
[root@aminglinux-01 ~]# . .bash_profile ^C 这个.这source是有一样的,加载配置文件里面的一些配置,
[root@aminglinux-01 ~]#
bash_profile 会自动调用bashrc。
[root@aminglinux-01 ~]# vim .bashrc
# .bashrc
# User specific aliases and functions
alias rm=‘rm -i‘
alias cp=‘cp -i‘
alias mv=‘mv -i‘
而bashrc又会自动调用/etc/bashrc
# Source global definitions
if [ -f /etc/bashrc ]; then
. /etc/bashrc
fi
~
~
".bashrc" 12L, 176C 1,1 全部
```
- 总结一下,系统的环境变量配置文件,有俩大类,俩个维度,一个是系统层次的,另一个是用户层次的。每一个用户的家目录下都会有.bash_profile 或者 .bashrc ,
- .bash_profile 和 .bashrc ,而这个 bash_profeile 和 bash.rc这俩种文件的区别在于.bash_profile是用户登录的时候,比如打开一个终端,输入ip输入用户名或者密码,就会自动加载这个profile,当然profile会自动调用bashrc
- 而bashrc 是执行shell脚本的时候,用户不用登录,只要执行shell 脚本,就会自动挑用bashrc里面的配置
- profile 是用户登录时候 会加载到
- bashrc 是用户 ,系统执行shell 脚本的时候 会执行里面的一些相关的变量,配置等
- 用户层次 用户家目录下的
1. ~/.bashrc
2. ~/.bash_profile
3. ~/.bash_history
4. ~/.bash_logout用来定义用户需要退出的时候做的一些操作,比如说用户每次退出时都想要删除他的命令历史,就可以要删除命令历史的命令放到.bash_logout里面
5.变量PS1
```
[root@aminglinux-01 ~]# vim /etc/bashrc
*)
[ -e /etc/sysconfig/bash-prompt-default ] && PROMPT_COMMAND=/etc/sysconfig/bash-prompt-default
;;
esac
fi
# Turn on parallel history
shopt -s histappend
history -a
# Turn on checkwinsize
shopt -s checkwinsize
[ "$PS1" = "\\s-\\v\\\$ " ] && PS1="[\u@\h \W]\\$ "
# You might want to have e.g. tty in prompt (e.g. more virtual machines)
# and console windows
# If you want to do so, just add e.g.
# if [ "$PS1" ]; then
# PS1="[\u@\h:\l \W]\\$ "
# fi
# to your custom modification shell script in /etc/profile.d/ directory
fi
if ! shopt -q login_shell ; then # We‘re not a login shell
# Need to redefine pathmunge, it get‘s undefined at the end of /etc/profile
pathmunge () {
41,7 43%
```
- PS1变量是什么
- [root@aminglinux-01 ~]#
- 其中最左侧是你登录的用户root,@是你的主机名hostname,然后是你所在的目录最后的层级
```
[root@aminglinux-01 ~]# vim /etc/bashrc
[root@aminglinux-01 ~]# cd /etc/sysconfig/network-scripts/
[root@aminglinux-01 network-scripts]# echo $PS1
[\u@\h \W]\$
u这个就是用户 ,h hostname W 是目录的最后一层
[root@aminglinux-01 network-scripts]#
```
- 把大W改成小w
```
[root@aminglinux-01 ~]# cd /etc/sysconfig/network-scripts/
[root@aminglinux-01 network-scripts]# echo $PS1
[\u@\h \W]\$
[root@aminglinux-01 network-scripts]# PS1=‘[\u@\h \w]\$‘
[root@aminglinux-01 /etc/sysconfig/network-scripts]#
现在这个目录改成了绝对路径了
```
```
[root@aminglinux-01 ~/123]#cd /tmp/
[root@aminglinux-01 /tmp]#ls
aming2
d6z
systemd-private-086e5c8bbc2d4cd9b9875b80a0804091-vmtoolsd.service-Ne2okB
systemd-private-df69b42524cf495da97759d5c0abbccb-vmtoolsd.service-3zT7bK
systemd-private-e031fa9e20494934b9848b750907442a-vmtoolsd.service-RI8yhR
vim-enhanced-7.4.160-1.el7_3.1.x86_64.rpm
yum_save_tx.2017-08-11.23-52.oRt6jf.yumtx
yum_save_tx.2017-08-12.00-28.o4N6Ss.yumtx
yum_save_tx.2017-08-12.00-31.QLxOxc.yumtx
yum_save_tx.2017-08-12.16-23.AT17pc.yumtx
yum_save_tx.2017-08-12.16-30.hWaWgQ.yumtx
yum_save_tx.2017-08-12.17-40.W2Mqi8.yumtx
yum_save_tx.2017-08-12.17-54.DgNopW.yumtx
yum_save_tx.2017-08-12.17-58.d_63wU.yumtx
zsh-5.0.2-25.el7_3.1.x86_64.rpm
[root@aminglinux-01 /tmp]#cd aming2/
[root@aminglinux-01 /tmp/aming2]#ls
[root@aminglinux-01 /tmp/aming2]#PS1=‘\u@\h \W\$‘
root@aminglinux-01 aming2#
PS1变量 把[]去掉了 现在就没有了
```
- 看着有点别扭,也可以改成<>
```
root@aminglinux-01 aming2#PS1=‘<\u@\h \W>\$‘
<root@aminglinux-01 aming2>#
```
- 现在变成<>了,这里最后的$,普通用户就是$,root就是#,用于区分
- 也可以让它带颜色,把PS1变量改成
 图片里的这样,就可以、
- 有了PS1 ,就有PS2
```
<root@aminglinux-01 aming2>#cd
<root@aminglinux-01 ~>#echo $PS2
>
<root@aminglinux-01 ~>#for i in ‘seq 1 10‘
> do
> echo $i
> done
seq 1 10
<root@aminglinux-01 ~>#PS2="#"
<root@aminglinux-01 ~>#echo $PS2
#
<root@aminglinux-01 ~>#for i in ‘seq 1 10‘
#do
#echo $i
#done
seq 1 10
<root@aminglinux-01 ~>#
```
## 拓展
---
bash_profile和bashrc区别
【.bash_profile 与 .bashrc 的区别】
.bash_profile is executed for login shells, while .bashrc is executed for interactive non-login shells.
【login shell 与 non-login shell 的区别】
1、当你直接在机器login界面登陆、使用ssh登陆或者su切换用户登陆时,.bash_profile 会被调用来初始化shell环境
Note:.bash_profile文件默认调用.bashrc文件
.bash_profile中有如下内容
if [ -f ~/.bashrc ]; then
. ~/.bashrc
fi
2、当你不登陆系统而使用ssh直接在远端执行命令,.bashrc 会被调用
3、当你已经登陆系统后,每打开一个新的Terminal时,.bashrc 都会被再次调用。
- 测试准备工作
hclient2主机hadoop用户家目录下执行
```
[hadoop@hclient2 ~]$ echo "invoke hclient2:~/.bashrc">>.bashrc
[hadoop@hclient2 ~]$ echo "invoke hclient2:~/.bash_profile">>.bash_profile
Login Shell
1、窗口登陆
Red Hat Enterprise Linux Server release 6.3 (Santiago)
Kernel 2.6.32-279.el6.x86_64 on an x86_64
hclient2 login: hadoop
Password:
Last login: Mon Feb 25 23:03:45 on tty1
invoke hclient2:~/.bashrc
invoke hclient2:~/.bash_profile
[hadoop@hclient2 ~]$
2、SSH 登陆
[hadoop@hserver ~]$ ssh hclient2
Last login: Mon Feb 25 22:42:19 2013 from hserver
invoke hclient2:~/.bashrc
invoke hclient2:~/.bash_profile
[hadoop@hclient2 ~]$
3、su 登陆
[root@hclient2 ~]# su - hadoop
invoke hclient2:~/.bashrc
invoke hclient2:~/.bash_profile
Non-login Shell:
Note: ssh ...[user@] hostname [command]
If command is specified, it is executed on the remote host instead of a login shell.
[hadoop@hserver ~]$ ssh hclient2 hostname
invoke hclient2:~/.bashrc
hclient2
```
- 【故】若要配置环境变量之类,最保险是写在 .bashrc 文件中。因为不管是登陆还是不登陆,该文件总会被调用!8.6 管道符和作业控制 8.7/8.8 shell变量 8.9 环境变量配置文件
标签:8.6 管道符和作业控制 8.7/8.8 shell变量 8.9 环境变量配置文件
原文地址:http://ch71smas.blog.51cto.com/13090095/1961516