hellopasswd
变量
环境变量
[root@localhost ~]# echo $PATH
/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/bin
[root@localhost ~]# echo $LANG
en_US.UTF-8
[root@localhost ~]# env
XDG_SESSION_ID=8
HOSTNAME=localhost.localdomain
SELINUX_ROLE_REQUESTED=
TERM=xterm
SHELL=/bin/bash
HISTSIZE=5000
SSH_CLIENT=192.168.9.1 2272 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=en_US.UTF-8
SELINUX_LEVEL_REQUESTED=
HISTCONTROL=ignoredups
SHLVL=1
HOME=/root
LOGNAME=root
SSH_CONNECTION=192.168.9.1 2272 192.168.9.134 22
LESSOPEN=||/usr/bin/lesspipe.sh %s
XDG_RUNTIME_DIR=/run/user/0
_=/usr/bin/env
[root@localhost ~]# set #set查看系统和用户自定义变量
#系统和用户自定义变量
自定义变量
- 自定义变量a=1
- 变量名规则:字母、数字下划线,首位不能为数字
- 变量值有特殊符号时需要用单引号括起来
- 变量的累加
- 全局变量export b=2
- unset变量
[root@localhost ~]# a=111
[root@localhost ~]# echo $a
111
[root@localhost ~]# set | grep 111
_=111
a=111
[root@localhost ~]# a="'a b c'"
[root@localhost ~]# echo $a
'a b c'
[root@localhost ~]# a='"a b c"'
[root@localhost ~]# echo $a
"a b c"
[root@localhost ~]# a="a$bc"
[root@localhost ~]# echo $a
a
[root@localhost ~]# a='a$bc'
[root@localhost ~]# echo $a
a$bc
[root@localhost ~]# a=1
[root@localhost ~]# b=2
[root@localhost ~]# echo $a$b
12
[root@localhost ~]# c='a$bc'
[root@localhost ~]# echo $c
a$bc
[root@localhost ~]# c=a$bc
[root@localhost ~]# echo $c
a
[root@localhost ~]# c="a$b"c
[root@localhost ~]# echo $c
a2c
本地变量
[root@localhost ~]# w
15:49:26 up 8:38, 2 users, load average: 0.00, 0.01, 0.05
USER TTY LOGIN@ IDLE JCPU PCPU WHAT
root pts/0 13:01 6.00s 0.45s 0.00s w
root pts/1 15:49 6.00s 0.02s 0.02s -bash
[root@localhost ~]# echo $SSH_TTY
/dev/pts/0
打开另一个终端
[root@localhost ~]# w
15:49:12 up 8:37, 2 users, load average: 0.00, 0.01, 0.05
USER TTY LOGIN@ IDLE JCPU PCPU WHAT
root pts/0 13:01 32.00s 0.45s 0.45s -bash
root pts/1 15:49 0.00s 0.03s 0.01s w
[root@localhost ~]# echo $SSH_TTY
/dev/pts/1
[root@localhost ~]# hello=world
[root@localhost ~]# echo $hello
world
打开另一个终端
[root@localhost ~]# echo $hello
返回原来的终端下,打开一个bash
[root@localhost ~]# bash
[root@localhost ~]# pstree
bash: pstree: command not found
[root@localhost ~]# yum provides "/*/pstree"
Loaded plugins: fastestmirror
Loading mirror speeds from cached hostfile
* epel: mirrors.ustc.edu.cn
epel/x86_64/filelists_db | 9.8 MB 00:00:11
extras/7/x86_64/filelists_db | 494 kB 00:00:01
psmisc-22.20-15.el7.x86_64 : Utilities for managing processes on your system
Repo : base
Matched from:
Filename : /usr/bin/pstree
[root@localhost ~]# yum install -y psmisc
Loaded plugins: fastestmirror
base | 3.6 kB 00:00:00
epel/x86_64/metalink | 6.3 kB 00:00:00
extras | 3.4 kB 00:00:00
updates | 3.4 kB 00:00:00
updates/7/x86_64/primary_db | 3.6 MB 00:00:03
Loading mirror speeds from cached hostfile
* epel: mirrors.ustc.edu.cn
Resolving Dependencies
--> Running transaction check
---> Package psmisc.x86_64 0:22.20-15.el7 will be installed
--> Finished Dependency Resolution
Dependencies Resolved
===================================================================================================
Package Arch Version Repository Size
===================================================================================================
Installing:
psmisc x86_64 22.20-15.el7 base 141 k
Transaction Summary
===================================================================================================
Install 1 Package
Total download size: 141 k
Installed size: 475 k
Downloading packages:
psmisc-22.20-15.el7.x86_64.rpm | 141 kB 00:00:00
Running transaction check
Running transaction test
Transaction test succeeded
Running transaction
Installing : psmisc-22.20-15.el7.x86_64 1/1
Verifying : psmisc-22.20-15.el7.x86_64 1/1
Installed:
psmisc.x86_64 0:22.20-15.el7
Complete!
[root@localhost ~]# pstree
systemd─┬─NetworkManager───2*[{NetworkManager}]
├─agetty
├─auditd───{auditd}
├─avahi-daemon───avahi-daemon
├─crond
├─dbus-daemon───{dbus-daemon}
├─firewalld───{firewalld}
├─iprdump
├─iprinit
├─iprupdate
├─lvmetad
├─master─┬─pickup
│ └─qmgr
├─polkitd───5*[{polkitd}]
├─rsyslogd───2*[{rsyslogd}]
├─sshd─┬─sshd───bash───bash───pstree
│ └─sshd───bash
├─systemd-journal
├─systemd-logind
├─systemd-udevd
└─tuned───4*[{tuned}]
这里我们通过├─sshd─┬─sshd───bash───bash───pstree可以看到,我们sshd远程登录获得了一个bash,通过bash上面打开了一个子shell,通过这个bash我们又运行了pstree这个进程
[root@localhost ~]# echo $hello
[root@localhost ~]# exit
exit
[root@localhost ~]# pstree
systemd─┬─NetworkManager───2*[{NetworkManager}]
├─agetty
├─auditd───{auditd}
├─avahi-daemon───avahi-daemon
├─crond
├─dbus-daemon───{dbus-daemon}
├─firewalld───{firewalld}
├─iprdump
├─iprinit
├─iprupdate
├─lvmetad
├─master─┬─pickup
│ └─qmgr
├─polkitd───5*[{polkitd}]
├─rsyslogd───2*[{rsyslogd}]
├─sshd─┬─sshd───bash───pstree
│ └─sshd───bash
├─systemd-journal
├─systemd-logind
├─systemd-udevd
└─tuned───4*[{tuned}]
[root@localhost ~]# echo $hello
world
全局变量export
[root@localhost ~]# export hello=world
[root@localhost ~]# echo $hello
world
[root@localhost ~]# bash
[root@localhost ~]# echo $hello
world
[root@localhost ~]# bash
[root@localhost ~]# echo $hello
world
打开另一个终端
[root@localhost ~]# pstree
systemd─┬─NetworkManager───2*[{NetworkManager}]
├─agetty
├─auditd───{auditd}
├─avahi-daemon───avahi-daemon
├─crond
├─dbus-daemon───{dbus-daemon}
├─firewalld───{firewalld}
├─iprdump
├─iprinit
├─iprupdate
├─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}]
[root@localhost ~]# echo $hello
这是因为两个终端都不属于同一个sshd,他们区别开来,并不冲突
返回原来的终端
[root@localhost ~]# export a=123
[root@localhost ~]# echo $a
123
[root@localhost ~]# exit
exit
[root@localhost ~]# echo $a
取消变量值unset
[root@localhost ~]# echo $hello
world
[root@localhost ~]# unset hello
[root@localhost ~]# echo $hello
不同用户登录,全局变量只对当前用户的shell和子shell生效,若要其他用户生效则可以到/etc/profile进行修改配置
环境变量配置文件
- /etc/profile用户环境变量,交互,登录才执行
- /etc/bashrc用户不用登录,执行shell就生效
当前用户下的目录
- ~/.bashrc
- ~/.bash_profile
- ~/.bash_history
- ~/.bash_logout
[root@localhost ~]# vi .bash_profile
# .bash_profile
# Get the aliases and functions
if [ -f ~/.bashrc ]; then
. ~/.bashrc
fi
# User specific environment and startup programs
PATH=$PATH:$HOME/bin
export PATH
. ~/.bashrc与source ~/.bashrc的作用是一样的,加载配置文件
[root@localhost ~]# vi .bashrc
# .bashrc
# User specific aliases and functions
alias rm='rm -i'
alias cp='cp -i'
alias mv='mv -i'
# Source global definitions
if [ -f /etc/bashrc ]; then
. /etc/bashrc
fi
[root@localhost ~]# vi /etc/bashrc
/PS1
[ "$PS1" = "\\s-\\v\\\$ " ] && PS1="[\u@\h \W]\\$ "
[root@localhost ~]#cd /etc/sysconfig/network-scripts/
[root@localhost /network-scripts]## echo $PS1
[\u@\h \W]\$
这里PS1的u表示当前用户,h为主机名,W为绝对路径最后一个目录
[root@localhost ~]# PS1='[\u@\h \w]\$'
[root@localhost ~]# cd /etc/sysconfig/network-scripts/
[root@localhost /etc/sysconfig/network-scripts]#
[root@localhost /etc/sysconfig/network-scripts]# PS1='\u@\h \w\$'
root@localhost /etc/sysconfig/network-scripts#
root@localhost /etc/sysconfig/network-scripts# PS1='<\u@\h \w>\$'
<root@localhost /etc/sysconfig/network-scripts>#
甚至还可以带颜色显示
root@localhost:/etc/sysconfig/network-scripts# PS1='[\[\033[01;32m\]\u@\h\[\033[00m\]:\[\033[01;36m\]\w\[\033[00m\]]\$'
[root@localhost:/etc/sysconfig/network-scripts]#
有PS1,那么也有PS2,而PS2如运行mysql等程序时,则会进入PS2界面
[root@localhost:~]# echo $PS2
>
[root@localhost:~]# for i in `seq 1 10`
> do
> echo $i
> done
1
2
3
4
5
6
7
8
9
10
[root@localhost:~]# for i in `seq 1 10`; do echo $i; done
1
2
3
4
5
6
7
8
9
10
[root@localhost:~]# PS2="#"
[root@localhost:~]# echo $PS2
#
[root@localhost:~]# for i in `seq 1 10`
#do
#echo $i
#done
1
2
3
4
5
6
7
8
9
10
修改于171116