shell
与 环境变量
1. 查看系统可用的shell
类型 :
[arthur@localhost ~]$ cat /etc/shells
/bin/sh
/bin/bash
/usr/bin/sh
/usr/bin/bash
/usr/bin/tmux
/bin/tmux
/usr/bin/zsh
/bin/zsh
2. 查看用户登陆时获得的shell
类型:
[arthur@localhost ~]$ cat /etc/passwd
root:x:0:0:root:/root:/bin/bash
bin:x:1:1:bin:/bin:/sbin/nologin
daemon:x:2:2:daemon:/sbin:/sbin/nologin
adm:x:3:4:adm:/var/adm:/sbin/nologin
...
rngd:x:973:973:Random Number Generator Daemon:/var/lib/rngd:/sbin/nologin
[arthur@localhost ~]$ cat /etc/passwd | grep arthur
arthur:x:1000:1000:arthur:/home/arthur:/bin/bash
3. shell
的变量功能:
3.1 可以使用echo
来显示变量内容:
[arthur@localhost ~]$ echo $PATH
/home/arthur/.local/bin:/home/arthur/bin:/home/arthur/.local/bin:/home/arthur/bin:/usr/local/bin:/usr/local/sbin:/usr/bin:/usr/sbin:/var/lib/snapd/snap/bin
[arthur@localhost ~]$ echo ${HOME}
/home/arthur
3.2 设置和修改变量内容:
可对变量赋值、修改、增添、取消
[arthur@localhost ~]$ vname1="This's is variable 1"
[arthur@localhost ~]$ echo $vname1
This's is variable 1
#单引号识别错误
[arthur@localhost ~]$ vname2='This\'s\ is\ variable\ 2' | echo $vname2
> ^C
#单引号识别错误
[arthur@localhost ~]$ vname2='This\'s\ is\ variable\ 2'
> ^C
[arthur@localhost ~]$ vname2=This\'s\ is\ variable\ 2
[arthur@localhost ~]$ echo $vname2
This's is variable 2
[arthur@localhost ~]$ echo $vname1 $vname2
This's is variable 1 This's is variable 2
#修改变量
[arthur@localhost ~]$ vname=variable
[arthur@localhost ~]$ echo $vname
variable
[arthur@localhost ~]$ vname=variables
[arthur@localhost ~]$ echo $vname
variables
#取消变量
[arthur@localhost ~]$ unset vname1 vname2
[arthur@localhost ~]$ echo $vname1 $vname2
变量内容删除、取代与替换:
[arthur@localhost ~]$ echo $PATH
/home/arthur/.local/bin:/home/arthur/bin:/home/arthur/.local/bin:/home/arthur/bin:/usr/local/bin:/usr/local/sbin:/usr/bin:/usr/sbin:/var/lib/snapd/snap/bin
#变量path 定义
[arthur@localhost ~]$ path=${PATH}
[arthur@localhost ~]$ echo $path
/home/arthur/.local/bin:/home/arthur/bin:/home/arthur/.local/bin:/home/arthur/bin:/usr/local/bin:/usr/local/sbin:/usr/bin:/usr/sbin:/var/lib/snapd/snap/bin
#######################################################################
#######################################################################
**删除**
##将首个含 /local/bin 的路径移除,`#`代表从变量内容的最前面开始向右删除,且仅删除最短的那个; `/`代表要被删除的部分,`#`代表由最前面开始向右删除,所以这里由`/`开始写起;
[arthur@localhost ~]$ echo ${path#/*local/bin:}
/home/arthur/bin:/home/arthur/.local/bin:/home/arthur/bin:/usr/local/bin:/usr/local/sbin:/usr/bin:/usr/sbin:/var/lib/snapd/snap/bin
[arthur@localhost ~]$ echo $PATH
/home/arthur/.local/bin:/home/arthur/bin:/home/arthur/.local/bin:/home/arthur/bin:/usr/local/bin:/usr/local/sbin:/usr/bin:/usr/sbin:/var/lib/snapd/snap/bin
#可以发现,上述删除操作并未改变变量`path`内容
[arthur@localhost ~]$ echo $path
/home/arthur/.local/bin:/home/arthur/bin:/home/arthur/.local/bin:/home/arthur/bin:/usr/local/bin:/usr/local/sbin:/usr/bin:/usr/sbin:/var/lib/snapd/snap/bin
#######################################################################
##删除前面所有目录,仅保留最后一个目录. 一个`#`代表仅删除最短的目录,两个`#`代表删除最长的目录
[arthur@localhost ~]$ echo ${path#/*:}
/home/arthur/bin:/home/arthur/.local/bin:/home/arthur/bin:/usr/local/bin:/usr/local/sbin:/usr/bin:/usr/sbin:/var/lib/snapd/snap/bin
[arthur@localhost ~]$ echo ${path}
/home/arthur/.local/bin:/home/arthur/bin:/home/arthur/.local/bin:/home/arthur/bin:/usr/local/bin:/usr/local/sbin:/usr/bin:/usr/sbin:/var/lib/snapd/snap/bin
[arthur@localhost ~]$ echo ${path##/*:}
/var/lib/snapd/snap/bin
[arthur@localhost ~]$ echo $path
/home/arthur/.local/bin:/home/arthur/bin:/home/arthur/.local/bin:/home/arthur/bin:/usr/local/bin:/usr/local/sbin:/usr/bin:/usr/sbin:/var/lib/snapd/snap/bin
[arthur@localhost ~]$ echo ${path#home*}
/home/arthur/.local/bin:/home/arthur/bin:/home/arthur/.local/bin:/home/arthur/bin:/usr/local/bin:/usr/local/sbin:/usr/bin:/usr/sbin:/var/lib/snapd/snap/bin:home/arthur/bin
[arthur@localhost ~]$ echo ${path}
/home/arthur/.local/bin:/home/arthur/bin:/home/arthur/.local/bin:/home/arthur/bin:/usr/local/bin:/usr/local/sbin:/usr/bin:/usr/sbin:/var/lib/snapd/snap/bin:home/arthur/bin
#########################################################################
#从后往前删除变量名称 `%`
[arthur@localhost ~]$ echo $path
/home/arthur/.local/bin:/home/arthur/bin:/home/arthur/.local/bin:/home/arthur/bin:/usr/local/bin:/usr/local/sbin:/usr/bin:/usr/sbin:/var/lib/snapd/snap/bin
#删除最后一个目录,从`:` 到`bin`为止
[arthur@localhost ~]$ echo ${path%:*bin}
/home/arthur/.local/bin:/home/arthur/bin:/home/arthur/.local/bin:/home/arthur/bin:/usr/local/bin:/usr/local/sbin:/usr/bin:/usr/sbin
#只保留第一个目录, `%%` 代表最长的符合字串
[arthur@localhost ~]$ echo $path
/home/arthur/.local/bin:/home/arthur/bin:/home/arthur/.local/bin:/home/arthur/bin:/usr/local/bin:/usr/local/sbin:/usr/bin:/usr/sbin:/var/lib/snapd/snap/bin
[arthur@localhost ~]$ echo ${path%%:*bin}
/home/arthur/.local/bin
#########################################################################
#########################################################################
**取代**
#将 `path` 变量内容 `sbin` 取代成大写 `SBIN`
[arthur@localhost ~]$ echo ${path}
/home/arthur/.local/bin:/home/arthur/bin:/home/arthur/.local/bin:/home/arthur/bin:/usr/local/bin:/usr/local/sbin:/usr/bin:/usr/sbin:/var/lib/snapd/snap/bin
#两条斜线中间的为旧字符串
[arthur@localhost ~]$ echo ${path/sbin/SBIN}
/home/arthur/.local/bin:/home/arthur/bin:/home/arthur/.local/bin:/home/arthur/bin:/usr/local/bin:/usr/local/SBIN:/usr/bin:/usr/sbin:/var/lib/snapd/snap/bin
[arthur@localhost ~]$ echo ${path}
/home/arthur/.local/bin:/home/arthur/bin:/home/arthur/.local/bin:/home/arthur/bin:/usr/local/bin:/usr/local/sbin:/usr/bin:/usr/sbin:/var/lib/snapd/snap/bin
#仅一条斜线时,仅首个变量内容会发生取代
[arthur@localhost ~]$ echo ${path/bin/BIN}
/home/arthur/.local/BIN:/home/arthur/bin:/home/arthur/.local/bin:/home/arthur/bin:/usr/local/bin:/usr/local/sbin:/usr/bin:/usr/sbin:/var/lib/snapd/snap/bin
#两条斜线,所有匹配的内容都会发生取代
[arthur@localhost ~]$ echo ${path}
/home/arthur/.local/bin:/home/arthur/bin:/home/arthur/.local/bin:/home/arthur/bin:/usr/local/bin:/usr/local/sbin:/usr/bin:/usr/sbin:/var/lib/snapd/snap/bin
[arthur@localhost ~]$ echo ${path//bin/BIN}
/home/arthur/.local/BIN:/home/arthur/BIN:/home/arthur/.local/BIN:/home/arthur/BIN:/usr/local/BIN:/usr/local/sBIN:/usr/BIN:/usr/sBIN:/var/lib/snapd/snap/BIN
3.3 环境变量
查看当前shell
环境下的环境变量: env
或者export
[arthur@localhost ~]$ env
LANG=en_US.UTF-8
...
USER=arthur
PWD=/home/arthur
SSH_ASKPASS=/usr/libexec/openssh/gnome-ssh-askpass
HOME=/home/arthur
LOGNAME=arthur
PATH=/home/arthur/.local/bin:/home/arthur/bin:/home/arthur/.local/bin:/home/arthur/bin:/usr/local/bin:/usr/local/sbin:/usr/bin:/usr/sbin:/var/lib/snapd/snap/bin
自定义变量转为环境变量:export
#export 变量
export PATH=${PATH}:/dir/to/des/bin
3.4 变量的有效范围
环境变量等价于全局变量(global variable); 自定义变量等价于局部变量(local variable)