UNIX:设置环境变量
时间:2020-01-09 10:43:37 来源:igfitidea点击:
显示所有环境变量及其值命令
要显示搜索路径,请执行:
echo $PATH
要显示提示设置,请执行:
echo $PS1
还有更多示例:
echo $USER echo $PWD echo $MAIL echo $JAVA_PATH echo $DB2INSTANCE
更改或者设置环境变量
您可以根据shell使用以下命令更改当前会话的环境变量。
对于Korn Shell(KSH)
语法如下:
var=value export var
要设置JAVA_PATH,请执行:
JAVA_PATH=/opt/jdk/bin export JAVA_PATH
对于Bourne shell(sh和bash)
语法如下:
export var=value
要设置PATH,请执行:
export PATH=$PATH:/opt/bin:/usr/local/bin:$HOME/bin
对于C shell(csh或者tcsh)
语法如下:
setenv var value
将EDITOR设置为vim,执行:
setenv EDITOR vim
示例:环境变量的UNIX C Shell启动配置文件
C shell使用以下文件:
/etc/csh.login
如果C shell是您的登录shell,则执行该命令。- $HOME/.cshrc和$HOME/.login每次C Shell启动时都会执行这些文件。 ~/.login是csh登录脚本,在登录时~/.cshrc之后,由登录shell读取。
上面的set或者setenv命令可以放在~/.cshrc或者~/.login文件中。
$HOME/.cshrc文件示例如下:
alias h history 25 alias j jobs -l alias la ls -a alias lf ls -FA alias ll ls -lA umask 22 set path = (/sbin /bin /usr/sbin /usr/bin /usr/games /usr/local/sbin /usr/local/bin $HOME/bin) setenv EDITOR vi setenv PAGER more setenv BLOCKSIZE K if ($?prompt) then # An interactive shell -- set some stuff up set filec set history = 100 set savehist = 100 set mail = (/var/mail/$USER) if ( $?tcsh ) then bindkey "^W" backward-delete-word bindkey -k up history-search-backward bindkey -k down history-search-forward endif endif # Traps CTRL-D's to avoid accidental system log off set ignoreeof # Set prompt set prompt = "[\!] %" # Sequentially keeps a buffer of your last events. set history=100 set savehist=100 # Stops C Shell from overwriting and destroying the information in an existing file. set noclobber
~/.login文件示例如下:
# Show fortune :) if ( -x /usr/games/fortune ) /usr/games/fortune # Sets the system variable TERM to recognize the xterm setenv TERM xterm # This command sets the time zone variable setenv TZ IST # set PATH setenv PATH /opt/gnu/bin:/bin/posix:/bin:/usr/bin:/usr/local/bin:/etc:/users/Hyman:. # set mail box set mail=/usr/mail/Hyman # alias bye is easier to remember alias bye logout alias c clear # read mail as soon as I get into the systems mutt
示例:环境变量的UNIX KSH Shell启动配置文件
KSH Shell使用以下文件:
/etc/profile
这个默认的系统文件由KSH执行,并设置默认的环境变量。$HOME/.profile
将您的自定义内容放入此文件。
ksh shell的示例$HOME/.profile:
PATH=/opt/gnu/bin:/bin/posix:/usr/bin:/usr/lib:/bin:/users/v/Hyman/bin MAIL=/usr/mail/Hyman HOME=/users/Hyman EDITOR=/opt/gnu/bin/vim START=~/.kshrc TERM=xterm # export it export ENV START EDITOR TERM PATH MAIL HOME stty sane susp ^Z # email notification if mail -e then echo "You have mail." fi # prompt PS1="$ " # Check system messages msgs -q # Allow terminal messages mesg y