1:环境变量
在bash shell中环境变量分为两类:
- 全局环境变量
- 局部环境变量
1.1:全局环境变量:
全局环境变量不仅对shell可见,对所有shell创建的子进程(也可认作为子shell)可见。这对那些子进程中获取父进程信息的程序很是有用。系统在开始bash会话之前就设置了一些全局的环境变量。可通过evn(printevn)命令查看:
zhangshuisheng:~ zhangshuisheng$ env
TERM_PROGRAM=Apple_Terminal
TERM=xterm-256color
SHELL=/bin/bash
TMPDIR=/var/folders/5n/ss3mb0bd6yj2vdn6tzdsgkdm0000gn/T/
Apple_PubSub_Socket_Render=/private/tmp/com.apple.launchd.x65L1DoqVs/Render
TERM_PROGRAM_VERSION=361.1
TERM_SESSION_ID=88C746B4-CC31-4B2A-9055-0E7378B20FF1
USER=zhangshuisheng
SSH_AUTH_SOCK=/private/tmp/com.apple.launchd.OehhRVr1Ma/Listeners
__CF_USER_TEXT_ENCODING=0x1F5:0x19:0x34
PATH=/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin
PWD=/Users/zhangshuisheng
LANG=zh_CN.UTF-8
ITERM_PROFILE=Default
XPC_FLAGS=0x0
XPC_SERVICE_NAME=0
COLORFGBG=7;0
SHLVL=2
HOME=/Users/zhangshuisheng
ITERM_SESSION_ID=w0t0p1:B3299AC3-4FFA-4304-A846-F504A06FED39
LOGNAME=zhangshuisheng
SECURITYSESSIONID=186a8
_=/usr/bin/env
OLDPWD=/Users/zhangshuisheng/work/shell
zhangshuisheng:~ zhangshuisheng$
可以明显看出,这些预设的全局环境变量大部分都是用户登入系统时设置的。显示其中的单个环境变量可使用echo输出,如:
zhangshuisheng:~ zhangshuisheng$ echo $HOME
/Users/zhangshuisheng
1.2:局部环境变量
局部变量只可在定义它的进程(或认为成shell)中可见,没有什么操作命令可以单独查看系统预设的局部环境变量,但是可以使用set命令查看所有的系统变量(包括全局和局部环境变量)如:
zhangshuisheng:~ zhangshuisheng$ set
Apple_PubSub_Socket_Render=/private/tmp/com.apple.launchd.x65L1DoqVs/Render
BASH=/bin/bash
BASH_ARGC=()
BASH_ARGV=()
BASH_LINENO=()
BASH_REMATCH=([0]="g")
BASH_SOURCE=()
BASH_VERSINFO=([0]="3" [1]="2" [2]="57" [3]="1" [4]="release" [5]="x86_64-apple-darwin15")
BASH_VERSION='3.2.57(1)-release'
COLORFGBG='7;0'
COLUMNS=80
DIRSTACK=()
EUID=501
GROUPS=()
HISTFILE=/Users/zhangshuisheng/.bash_sessions/88C746B4-CC31-4B2A-9055-0E7378B20FF1.historynew
HISTFILESIZE=500
HISTSIZE=500
HOME=/Users/zhangshuisheng
HOSTNAME=zhangshuisheng.lan
HOSTTYPE=x86_64
IFS=$' \t\n'
ITERM_PROFILE=Default
ITERM_SESSION_ID=w0t0p1:B3299AC3-4FFA-4304-A846-F504A06FED39
LANG=zh_CN.UTF-8
LINES=24
LOGNAME=zhangshuisheng
MACHTYPE=x86_64-apple-darwin15
MAILCHECK=60
OLDPWD=/Users/zhangshuisheng/work/shell
OPTERR=1
OPTIND=1
OSTYPE=darwin15
PATH=/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin
PIPESTATUS=([0]="0")
PPID=9164
PROMPT_COMMAND=update_terminal_cwd
PS1='\h:\W \u\$ '
PS2='> '
PS4='+ '
PWD=/Users/zhangshuisheng
SECURITYSESSIONID=186a8
SHELL=/bin/bash
SHELLOPTS=braceexpand:emacs:hashall:histexpand:history:interactive-comments:monitor
SHELL_SESSION_DID_HISTORY_CHECK=1
SHELL_SESSION_DID_INIT=1
SHELL_SESSION_DIR=/Users/zhangshuisheng/.bash_sessions
SHELL_SESSION_FILE=/Users/zhangshuisheng/.bash_sessions/88C746B4-CC31-4B2A-9055-0E7378B20FF1.session
SHELL_SESSION_HISTFILE=/Users/zhangshuisheng/.bash_sessions/88C746B4-CC31-4B2A-9055-0E7378B20FF1.history
SHELL_SESSION_HISTFILE_NEW=/Users/zhangshuisheng/.bash_sessions/88C746B4-CC31-4B2A-9055-0E7378B20FF1.historynew
SHELL_SESSION_HISTFILE_SHARED=/Users/zhangshuisheng/.bash_history
SHELL_SESSION_HISTORY=1
SHELL_SESSION_TIMESTAMP_FILE=/Users/zhangshuisheng/.bash_sessions/_expiration_check_timestamp
SHLVL=2
SSH_AUTH_SOCK=/private/tmp/com.apple.launchd.OehhRVr1Ma/Listeners
TERM=xterm-256color
TERM_PROGRAM=Apple_Terminal
TERM_PROGRAM_VERSION=361.1
TERM_SESSION_ID=88C746B4-CC31-4B2A-9055-0E7378B20FF1
TMPDIR=/var/folders/5n/ss3mb0bd6yj2vdn6tzdsgkdm0000gn/T/
UID=501
USER=zhangshuisheng
XPC_FLAGS=0x0
XPC_SERVICE_NAME=0
_=clear
__CF_USER_TEXT_ENCODING=0x1F5:0x19:0x34
shell_session_delete_expired ()
{
if ( [ ! -e "$SHELL_SESSION_TIMESTAMP_FILE" ] || [ -z $(find "$SHELL_SESSION_TIMESTAMP_FILE" -mtime -1d) ] ); then
local expiration_lock_file="$SHELL_SESSION_DIR/_expiration_lockfile";
if shlock -f "$expiration_lock_file" -p $$; then
echo -n 'Deleting expired sessions...';
local delete_count=$(find "$SHELL_SESSION_DIR" -type f -mtime +2w -print -delete | wc -l);
[ "$delete_count" -gt 0 ] && echo $delete_count' completed.' || echo 'none found.';
touch "$SHELL_SESSION_TIMESTAMP_FILE";
rm "$expiration_lock_file";
fi;
fi
}
shell_session_history_allowed ()
{
if [ -n "$HISTFILE" ]; then
local allowed=0;
if shopt -q histappend || [ -n "$HISTTIMEFORMAT" ]; then
allowed=${SHELL_SESSION_HISTORY:-0};
else
allowed=${SHELL_SESSION_HISTORY:=1};
fi;
if [ $allowed -eq 1 ]; then
return 0;
fi;
fi;
return 1
}
shell_session_history_check ()
{
if [ ${SHELL_SESSION_DID_HISTORY_CHECK:-0} -eq 0 ]; then
SHELL_SESSION_DID_HISTORY_CHECK=1;
if shell_session_history_allowed; then
shell_session_history_enable;
fi;
if [ "$PROMPT_COMMAND" = "shell_session_history_check" ]; then
unset PROMPT_COMMAND;
else
if [[ $PROMPT_COMMAND =~ (.*)(; *shell_session_history_check *| *shell_session_history_check *; *)(.*) ]]; then
PROMPT_COMMAND="${BASH_REMATCH[1]}${BASH_REMATCH[3]}";
fi;
fi;
fi
}
shell_session_history_enable ()
{
touch "$SHELL_SESSION_HISTFILE_NEW";
HISTFILE="$SHELL_SESSION_HISTFILE_NEW";
SHELL_SESSION_HISTORY=1
}
shell_session_save ()
{
if [ -n "$SHELL_SESSION_FILE" ]; then
echo -n 'Saving session...';
echo echo Restored session: $(date) >|"$SHELL_SESSION_FILE";
declare -F shell_session_save_user_state > /dev/null && shell_session_save_user_state;
shell_session_history_allowed && shell_session_save_history;
echo 'completed.';
fi
}
shell_session_save_history ()
{
shell_session_history_enable;
history -a;
if [ -f "$SHELL_SESSION_HISTFILE_SHARED" ] && [ ! -s "$SHELL_SESSION_HISTFILE" ]; then
echo -ne '\n...copying shared history...';
cp "$SHELL_SESSION_HISTFILE_SHARED" "$SHELL_SESSION_HISTFILE";
fi;
echo -ne '\n...saving history...';
cat "$SHELL_SESSION_HISTFILE_NEW" >> "$SHELL_SESSION_HISTFILE_SHARED";
cat "$SHELL_SESSION_HISTFILE_NEW" >> "$SHELL_SESSION_HISTFILE";
: >|"$SHELL_SESSION_HISTFILE_NEW";
if [ -n "$HISTFILESIZE" ]; then
echo -n 'truncating history files...';
HISTFILE="$SHELL_SESSION_HISTFILE_SHARED";
HISTFILESIZE="$HISTFILESIZE";
HISTFILE="$SHELL_SESSION_HISTFILE";
HISTFILESIZE="$size";
HISTFILE="$SHELL_SESSION_HISTFILE_NEW";
fi;
echo -ne '\n...'
}
shell_session_update ()
{
shell_session_save && shell_session_delete_expired
}
update_terminal_cwd ()
{
local url_path='';
{
local i ch hexch LC_CTYPE=C LC_ALL=;
for ((i = 0; i < ${#PWD}; ++i))
do
ch="${PWD:i:1}";
if [[ "$ch" =~ [/._~A-Za-z0-9-] ]]; then
url_path+="$ch";
else
printf -v hexch "%02X" "'$ch";
url_path+="%${hexch: -2:2}";
fi;
done
};
printf '\e]7;%s\a' "file://$HOSTNAME$url_path"
}
从输出可以看出,不仅包含了操作env命令时输出的全局的系统变量,还有输出其他的局部变量。
2:设置环境变量
2.1:设置局部变量
zhangshuisheng:~ zhangshuisheng$ test="hello shell"
zhangshuisheng:~ zhangshuisheng$ echo $test
hello shell
zhangshuisheng:~ zhangshuisheng$ bash
bash-3.2$ echo $test
bash-3.2$
注:示例展示了局部环境变量的设置(test="hello shell")和可见性(局部环境变量尽在创建他的shell中可用)
2.2:设置全局环境变量(export)
zhangshuisheng:~ zhangshuisheng$ export text1="a beautiful day"
zhangshuisheng:~ zhangshuisheng$ echo $text1
a beautiful day
zhangshuisheng:~ zhangshuisheng$ bash
bash-3.2$ echo $text1
a beautiful day
bash-3.2$ exit
exit
zhangshuisheng:~ zhangshuisheng$ export test
zhangshuisheng:~ zhangshuisheng$ echo $test
hello shell
zhangshuisheng:~ zhangshuisheng$ bash
bash-3.2$ echo $test
hello shell
bash-3.2$
注:全局环境变量可以通过export来设置。通过上面示例可看出:可以使用export直接来设置一个全局环境变量,也可以使用它把局部环境变量导出为全局环境变量。全局环境变量在所有子进程(shell)中都可见
2.3:删除环境变量(unset)
zhangshuisheng:~ zhangshuisheng$ unset test
zhangshuisheng:~ zhangshuisheng$ echo $test
zhangshuisheng:~ zhangshuisheng$ unset $text1
zhangshuisheng:~ zhangshuisheng$ echo $text1
a beautiful day
zhangshuisheng:~ zhangshuisheng$ unset text1
zhangshuisheng:~ zhangshuisheng$ echo $text1
zhangshuisheng:~ zhangshuisheng$
注:通过上面实例可以看出,unset在删除环境变量时直接加载变量名前面空格隔开,变量名前不能加$