shell维护跟踪特定系统信息的环境变量,这些系统信息包含:
系统的名称
登录到系统中的用户名
用户的系统ID(称为UID)
用户的默认根目录
shell查找程序使用的搜索路径等,
使用set
命令,可以显示活动环境变量的完整列表:
[root@hadoop ~]# set
BASH=/bin/bash
BASH_ALIASES=()
BASH_ARGC=()
BASH_ARGV=()
BASH_CMDS=()
BASH_LINENO=()
BASH_SOURCE=()
COLUMNS=115
DIRSTACK=()
EUID=0
GROUPS=()
HISTCONTROL=ignoredups
HISTFILE=/root/.bash_history
HISTFILESIZE=1000
HISTSIZE=1000
HOME=/root
HOSTNAME=hadoop
HOSTTYPE=x86_64
ID=0
IFS=$' \t\n'
JAVA_HOME=/usr/java/default
LANG=zh_CN.UTF-8
LESSOPEN='||/usr/bin/lesspipe.sh %s'
LINES=27
LOGNAME=root
MACHTYPE=x86_64-redhat-linux-gnu
MAIL=/var/spool/mail/root
MAILCHECK=60
OPTERR=1
OPTIND=1
OSTYPE=linux-gnu
PATH=/opt/hive-2.3.5/conf:/opt/hive-2.3.5/bin:/opt/sqoop-1.4.7/bin:/usr/java/default/bin:/opt/hadoop/hadoop-2.7.6/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/bin
PIPESTATUS=([0]="0")
PPID=13125
PROMPT_COMMAND='printf "\033]0;%s@%s:%s\007" "${USER}" "${HOSTNAME%%.*}" "${PWD/#$HOME/~}"'
PS1='[\u@\h \W]\$ '
PS2='> '
PS4='+ '
PWD=/root
SELINUX_LEVEL_REQUESTED=
SELINUX_ROLE_REQUESTED=
SELINUX_USE_CURRENT_RANGE=
SHELL=/bin/bash
SSH_CLIENT='192.168.119.1 62729 22'
SSH_CONNECTION='192.168.119.1 62729 192.168.119.128 22'
SSH_TTY=/dev/pts/0
TERM=xterm
UID=0
USER=root
XDG_RUNTIME_DIR=/run/user/0
XDG_SESSION_ID=4
_=clear
colors=/root/.dircolors
通过使用美元符号开头的环境变量可以在脚本中引用这些环境变量,例如:
[root@hadoop dafu]# cat 1.sh
#!/bin/bash
echo "user is :"$USER
echo "UID is :"$UID
echo "home is :$HOME"
[root@hadoop dafu]# ./1.sh
user is :root
UID is :0
home is :/root
这里将美元符号认为是引用环境变量,那么如何显示没有符号本身呢?需要使用反斜杠转义:
[root@hadoop dafu]# cat 1.sh
#!/bin/bash
echo "user is :"$USER
echo "UID is :"$UID
echo "home is :$HOME"
echo "this is \$HOME"
[root@hadoop dafu]# sh 1.sh
user is :root
UID is :0
home is :/root
this is $HOME
也可以使用${var}
格式来引用变量,包围变量名的大括号与美元符号区分开来,例如:
[root@hadoop dafu]# echo "home is :${HOME}"
home is :/root