Linux环境变量需要知道的内容:
- 什么是环境变量;
- 创建自己的环境变量;
- 删除环境变量;
- 默认shell环境变量;
- 设置PATH环境变量;
- 定位环境文件;
环境变量
Environment variable,定义系统会话和环境信息的变量或者参数。可以快速访问和修改内存中的数据。
系统的环境变量都用大写,以区分普通用户的环境变量。
全局变量
全局变量对shell会话和所有子shell会话都有效。局部变量只对生成他们的shell会话有效。
查看命令:env 或printenv
查看某个环境变量:printenv “变量名”,或者 echo $变量名
提示: 变量名前加$,可以将变量用于命令行中,比如需要引用环境变量时,需要加$符号.
shaphicprb13137:~ # env
LESSKEY=/etc/lesskey.bin
MALLOC_MMAP_MAX_=65536
MANPATH=/usr/share/man:/usr/local/man:/usr/local/share/man
NNTPSERVER=news
XDG_SESSION_ID=844641
HOSTNAME=shaphicprb13137
XKEYSYMDB=/usr/X11R6/lib/X11/XKeysymDB
SHELL=/bin/bash //bash shell的全路径名
TERM=xterm
HOST=shaphicprb13137 //当前主机名称
HISTSIZE=0 //history命令可以显示的命令行数
PROFILEREAD=true
SSH_CLIENT=10.164.148.237 58175 22 //ssh的客户端地址和端口
MORE=-sl
SSH_TTY=/dev/pts/0 //SSH默认使用的tty设备
MALLOC_TRIM_THRESHOLD_=131072
LC_ALL=en_US.UTF-8
USER=root
LS_COLORS=no=00:fi=00:di=01;34:ln=00;36:pi=40;33:so=01;35:do=01;35:bd=40;33;01:cd=40;33;01:or=41;33;01:ex=00;32:*.cmd=00;32:*.exe=01;32:*.com=01;32:*.bat=01;32:*.btm=01;32:*.dll=01;32:*.tar=00;31:*.tbz=00;31:*.tgz=00;31:*.rpm=00;31:*.deb=00;31:*.arj=00;31:*.taz=00;31:*.lzh=00;31:*.lzma=00;31:*.zip=00;31:*.zoo=00;31:*.z=00;31:*.Z=00;31:*.gz=00;31:*.bz2=00;31:*.tb2=00;31:*.tz2=00;31:*.tbz2=00;31:*.xz=00;31:*.avi=01;35:*.bmp=01;35:*.fli=01;35:*.gif=01;35:*.jpg=01;35:*.jpeg=01;35:*.mng=01;35:*.mov=01;35:*.mpg=01;35:*.pcx=01;35:*.pbm=01;35:*.pgm=01;35:*.png=01;35:*.ppm=01;35:*.tga=01;35:*.tif=01;35:*.xbm=01;35:*.xpm=01;35:*.dl=01;35:*.gl=01;35:*.wmv=01;35:*.aiff=00;32:*.au=00;32:*.mid=00;32:*.mp3=00;32:*.ogg=00;32:*.voc=00;32:*.wav=00;32:
XNLSPATH=/usr/share/X11/nls
MALLOC_TOP_PAD_=131072
HOSTTYPE=x86_64 //主机类型
QEMU_AUDIO_DRV=pa
MALLOC_ARENA_MAX=2
FROM_HEADER=
PAGER=less
CSHEDIT=emacs
XDG_CONFIG_DIRS=/etc/xdg
MINICOM=-c on
LIBGL_DEBUG=quiet
PATH=/sbin:/usr/sbin:/usr/local/sbin:/root/bin:/usr/local/bin:/usr/bin:/bin:/usr/bin/X11:/usr/games //冒号分割的目录列表,作为cd命令的搜索路径
MAIL=/var/mail/root //当前用户收件箱的文件名
CPU=x86_64
PWD=/root
INPUTRC=/etc/inputrc
LANG=en_US
PYTHONSTARTUP=/etc/pythonstart
AUDIODRIVER=pulseaudio
GPG_TTY=/dev/pts/0
HOME=/root //当前主目录
SHLVL=4 //shell的层级
QT_SYSTEM_DIR=/usr/share/desktop-data
OSTYPE=linux
LESS_ADVANCED_PREPROCESSOR=no
SDL_AUDIODRIVER=pulse
ALSA_CONFIG_PATH=/etc/alsa-pulse.conf
LS_OPTIONS=-A -N --color=tty -T 0
WINDOWMANAGER=env GNOME_SHELL_SESSION_MODE=classic SLE_CLASSIC_MODE=1 gnome-session --session gnome-classic
LOGNAME=root
MACHTYPE=x86_64-suse-linux
LESS=-M -I -R
G_FILENAME_ENCODING=@locale,UTF-8,ISO-8859-15,CP1252
CVS_RSH=ssh
SSH_CONNECTION=10.164.148.237 58175 100.107.250.59 22
XDG_DATA_DIRS=/usr/share
LESSOPEN=lessopen.sh %s
DISPLAY=localhost:10.0
XDG_RUNTIME_DIR=/run/user/0
XAUTHLOCALHOSTNAME=shaphicprb13137
LESSCLOSE=lessclose.sh %s %s
MALLOC_MMAP_THRESHOLD_=131072
G_BROKEN_FILENAMES=1
COLORTERM=1
_=/usr/bin/env
OLDPWD=/root/.ssh
You have new mail in /var/mail/root
shaphicprb13137:~ # printenv HOME
/root
shaphicprb13137:~ # echo $HOME
/root
shaphicprb13137:~ # ls $HOME
.bash_history .dbus ftp-0.17-66.x86_64.rpm .local pkcs11-helper-1.11-5.3.x86_64.rpm Templates .viminfo .Xauthority
.bash_profile Desktop .gnupg Music PreInstall test2 .xauth83zp1l .xauthPhGsb8
bin Documents .ICEauthority .mysql_history PreInstall_NCE.zip text2.txt .xauthGLFA5f .xsession-errors-:0
.cache Downloads inst-sys openvpn-2.3.8-12.1.x86_64.rpm Public Videos .xauthjekM76
.config .esd_auth libpkcs11-helper1-1.11-5.3.x86_64.rpm Pictures .ssh .vim .xauthN4KDjI
局部变量
全局变量对shell会话和所有子shell会话都有效。局部变量只对生成他们的shell会话有效。
Linux中查看局部变量有些复杂,并没有一个专有局部变量查看命令。可以通过set命令显示某个特定进程设置的所有环境变量,包括局部变量、全局变量以及用户自定义变量。
shaphicprb13137:~ # set
ALSA_CONFIG_PATH=/etc/alsa-pulse.conf
AUDIODRIVER=pulseaudio
BASH=/bin/bash
BASHOPTS=checkwinsize:cmdhist:complete_fullquote:expand_aliases:extglob:extquote:force_fignore:histappend:interactive_comments:progcomp:promptvars:sourcepath
BASH_ALIASES=()
BASH_ARGC=()
BASH_ARGV=()
BASH_CMDS=()
……
quote ()
{
local quoted=${1//\'/\'\\\'\'};
printf "'%s'" "$quoted"
}
quote_readline ()
{
local quoted;
_quote_readline_by_ref "$1" ret;
printf %s "$ret"
}
remount ()
{
/bin/mount -o remount,${1+"$@"}
}
spwd ()
{
( IFS=/;
set $PWD;
if test $# -le 3; then
echo "$PWD";
else
eval echo \"..\${$(($#-1))}/\${$#}\";
fi )
}
startx ()
{
test -x /usr/bin/startx || {
echo "No startx installed" 1>&2;
return 1
};
/usr/bin/startx ${1+"$@"} 2>&1 | tee $HOME/.xsession-errors
}
用户自定义变量
设置局部用户自定义变量:赋值
只需要用=赋值即可,局部的变量只对本shell进程内有效。赋值有空格和符号时,要加双引号。不可以有空格!!
定义局部自定义变量的意义是,在本shell内可以随时调用该变量。
shaphicprb13137:~ # myvar=HelloLinux
You have new mail in /var/mail/root
shaphicprb13137:~ # echo $myvar
HelloLinux
shaphicprb13137:~ # myvar2="Hello Linux!"
shaphicprb13137:~ # echo $myvar2
Hello Linux!
shaphicprb13137:~ #
注意 这样定义的变量只在本shell内有效,其他子shell或者父shell内无效。如下:
shaphicprb13137:~ # bash
shaphicprb13137:~ # ps --forest
PID TTY TIME CMD
165785 pts/0 00:00:00 bash
191218 pts/0 00:00:00 \_ bash
193182 pts/0 00:00:00 \_ bash
194820 pts/0 00:00:00 \_ bash
153722 pts/0 00:00:00 \_ bash
160557 pts/0 00:00:00 \_ ps
shaphicprb13137:~ # echo $myvar
shaphicprb13137:~ # echo $myvar2
shaphicprb13137:~ #
设置全局环境变量:export
步骤:先创建一个环境变量,然后export导出到全局变量。导出时,直接导出变量名,不需要加$
使用$的诀窍:使用变量时,需要加$;操作变量时,不需要加$。
shaphicprb13137:~ # ps --forest
PID TTY TIME CMD
165785 pts/0 00:00:00 bash
191218 pts/0 00:00:00 \_ bash
193182 pts/0 00:00:00 \_ bash
194820 pts/0 00:00:00 \_ bash
193059 pts/0 00:00:00 \_ bash
196424 pts/0 00:00:00 \_ ps
shaphicprb13137:~ # echo $myvar
shaphicprb13137:~ # exit
exit
You have new mail in /var/mail/root
shaphicprb13137:~ # echo $myvar
HelloLinux
shaphicprb13137:~ # echo $myvar2
Hello Linux!
shaphicprb13137:~ # export myvar
shaphicprb13137:~ # export myvar2
shaphicprb13137:~ # bash
shaphicprb13137:~ # ps --forest
PID TTY TIME CMD
165785 pts/0 00:00:00 bash
191218 pts/0 00:00:00 \_ bash
193182 pts/0 00:00:00 \_ bash
194820 pts/0 00:00:00 \_ bash
228562 pts/0 00:00:00 \_ bash
233863 pts/0 00:00:00 \_ ps
shaphicprb13137:~ # echo $myvar
HelloLinux
shaphicprb13137:~ # echo $myvar2
Hello Linux!
shaphicprb13137:~ #
删除环境变量:unset
上述步骤中设置的变量可以通过unset “变量名”的方式删除。
shaphicprb13137:~ #
shaphicprb13137:~ # echo $myvar
HelloLinux
shaphicprb13137:~ # echo $myvar2
Hello Linux!
shaphicprb13137:~ # unset myvar
shaphicprb13137:~ # unset myvar2
shaphicprb13137:~ # ps --forest
PID TTY TIME CMD
165785 pts/0 00:00:00 bash
191218 pts/0 00:00:00 \_ bash
193182 pts/0 00:00:00 \_ bash
194820 pts/0 00:00:00 \_ bash
228562 pts/0 00:00:00 \_ bash
63321 pts/0 00:00:00 \_ ps
shaphicprb13137:~ # exit
exit
You have new mail in /var/mail/root
shaphicprb13137:~ # exit
exit
You have new mail in /var/mail/root
shaphicprb13137:~ # echo $myvar
shaphicprb13137:~ # echo $myvar2
shaphicprb13137:~ # ps --forest
PID TTY TIME CMD
165785 pts/0 00:00:00 bash
191218 pts/0 00:00:00 \_ bash
193182 pts/0 00:00:00 \_ bash
85964 pts/0 00:00:00 \_ ps
shaphicprb13137:~ #
注意:在子进程中删除的全局变量,只对子进程生效,父进程不变。
修改path环境变量:临时修改
方法一:添加指定目录到PATH
PATH=$PATH:“目录”
方法二:添加当前目录到PATH
PATH=$PATH:.
两种方法都是临时修改,退出系统或者重启系统后则会丢失.
shaphicprb13137:~ # echo $PATH
/sbin:/usr/sbin:/usr/local/sbin:/root/bin:/usr/local/bin:/usr/bin:/bin:/usr/bin/X11:/usr/games
You have new mail in /var/mail/root
shaphicprb13137:~ # PATH=$PATH:/etc
shaphicprb13137:~ # echo $PATH
/sbin:/usr/sbin:/usr/local/sbin:/root/bin:/usr/local/bin:/usr/bin:/bin:/usr/bin/X11:/usr/games:/etc
shaphicprb13137:/etc # PATH=$PATH:.
shaphicprb13137:/etc # echo $PATH
/sbin:/usr/sbin:/usr/local/sbin:/root/bin:/usr/local/bin:/usr/bin:/bin:/usr/bin/X11:/usr/games:/etc:.
shaphicprb13137:/etc #
环境变量持久化:永久性修改环境变量
Linux在启动时,会默认在几个文件中查找命令,这些文件就是启动文件或者环境文件。我们将环境变量写入启动文件中,每次登陆或者启动系统,都会执行文件中的变量,就实现了环境变量的持久化。
Shell的启动文件有5个,分别是一个系统启动文件/etc/profile,4个用户启动文件$HOME/下的4个文件,这四个文件是可以根据用户需求自己定制的。
- /etc/profile
- $HOME/.bash_profile
- $HOME/.bashrc
- $HOME/.bash_login
- $HOME/.profile
Linux推荐通过修改用户启动文件来实现环境变量持久化。或者在/etc/profile.d目录中创建一个.sh结尾的文件,把所有新的或修改过得和全局环境变量放在文件中。
系统启动文件/etc/profile
/etc/profile是bash shell默认的主启动文件,只要启动就会执行其中的命令。其中会调度其他目录下的一下命令文件。
例如:
shaphicprb13137:/etc # cat /etc/profile
# /etc/profile for SUSE Linux
#
# PLEASE DO NOT CHANGE /etc/profile. There are chances that your changes
# will be lost during system upgrades. Instead use /etc/profile.local for
# your local settings, favourite global aliases, VISUAL and EDITOR
# variables, etc ...
#
# Check which shell is reading this file
#
if test -f /proc/mounts ; then
if ! is=$(readlink /proc/$$/exe 2>/dev/null) ; then
case "$0" in
*pcksh) is=ksh ;;
*) is=sh ;;
esac
fi
case "$is" in
*/bash) is=bash
case "$0" in
sh|-sh|*/sh)
is=sh ;;
esac ;;
*/ash) is=ash ;;
*/dash) is=ash ;;
*/ksh) is=ksh ;;
*/ksh93) is=ksh ;;
*/pdksh) is=ksh ;;
*/mksh) is=ksh ;;
*/lksh) is=ksh ;;
*/*pcksh) is=ksh ;;
*/zsh) is=zsh ;;
*/*) is=sh ;;
esac
#
# `r' in $- occurs *after* system files are parsed
#
for a in $SHELL ; do
case "$a" in
*/r*sh)
readonly restricted=true ;;
-r*|-[!-]r*|-[!-][!-]r*)
readonly restricted=true ;;
--restricted)
readonly restricted=true ;;
esac
done
unset a
else
is=sh
fi
#
# Call common progams from /bin or /usr/bin only
#
path ()
{
command -p ${1+"$@"}
}
#
# Initialize terminal
#
tty=`path tty 2> /dev/null`
test $? -ne 0 && tty=""
if test -O "$tty" -a -n "$PS1"; then
test -z "${TERM}" && { TERM=linux; export TERM; }
test "${TERM}" = "unknown" && { TERM=linux; export TERM; }
test "${TERM}" = "ibm327x" && { TERM=dumb; export TERM; }
# Do not change settings on local line if connected to remote
if test -z "$SSH_TTY" -a "${TERM}" != "dumb" ; then
path stty sane cr0 pass8 dec
path tset -I -Q
fi
# on iSeries virtual console, detect screen size and terminal
if test -d /proc/iSeries -a \( "$tty" = "/dev/tty1" -o "$tty" = "/dev/console" \) ; then
LINES=24
COLUMNS=80
export LINES COLUMNS TERM
eval `path initviocons -q -e`
fi
fi
unset TERMCAP
#
# Time until a complete key sequence must have arrived
#
#ESCDELAY=2000
#export ESCDELAY
#
# The user file-creation mask
#
# The global umask value is stored in /etc/login.defs and
# will be set by pam_umask.so (see "man pam_umask").
#umask 022
#
# Setup for gzip and (t)csh users
#
if test -z "$PROFILEREAD" ; then
# GZIP=-9
# export GZIP
CSHEDIT=emacs
export CSHEDIT
fi
#
# ksh/ash sometimes do not know
#
test -z "$UID" && readonly UID=`path id -ur 2> /dev/null`
test -z "$EUID" && readonly EUID=`path id -u 2> /dev/null`
test -z "$USER" && USER=`path id -un 2> /dev/null`
test -z "$MAIL" && MAIL=/var/spool/mail/$USER
if test -x /bin/uname ; then
test -z "$HOST" && HOST=`/bin/uname -n`
test "$HOST" = "localhost" && HOST=`/bin/uname -n`
test -z "$CPU" && CPU=`/bin/uname -m`
fi
# Remark: /proc/sys/kernel/domainname and the program domainname
# its self will provide the NIS/YP domainname, see domainname(8).
if test -s /etc/HOSTNAME ; then
test -z "$HOSTNAME" && HOSTNAME=`cat /etc/HOSTNAME`
else
test -z "$HOSTNAME" && HOSTNAME=$HOST
fi
test -z "$LOGNAME" && LOGNAME=$USER
case "$CPU" in
i?86) HOSTTYPE=i386 ;;
*) HOSTTYPE=${CPU} ;;
esac
OSTYPE=linux
MACHTYPE=${CPU}-suse-${OSTYPE}
# Do NOT export UID, EUID, USER, and LOGNAME
export MAIL HOST CPU HOSTNAME HOSTTYPE OSTYPE MACHTYPE
#
# You may use /etc/initscript, /etc/profile.local or the
# ulimit package instead to set up ulimits and your PATH.
#
# if test "$is" != "ash" -a ! -r /etc/initscript; then
# ulimit -Sc 0 # don't create core files
# ulimit -Sd $(ulimit -Hd)
# ulimit -Ss $(ulimit -Hs)
# ulimit -Sm $(ulimit -Hm)
# fi
#
# Make path more comfortable
#
if test -z "$PROFILEREAD" ; then
PATH=/usr/local/bin:/usr/bin:/bin
if test "$HOME" != "/" ; then
for dir in $HOME/bin/$CPU $HOME/bin ; do
test -d $dir && PATH=$dir:$PATH
done
fi
if test "$UID" = 0 ; then
test -d /opt/kde3/sbin && PATH=/opt/kde3/sbin:$PATH
PATH=/sbin:/usr/sbin:/usr/local/sbin:$PATH
fi
for dir in /usr/X11/bin \
/usr/bin/X11 \
/usr/X11R6/bin \
/var/lib/dosemu \
/usr/games \
/opt/bin \
/opt/kde3/bin \
/opt/kde2/bin \
/opt/kde/bin \
/usr/openwin/bin \
/opt/cross/bin
do
test -d $dir && PATH=$PATH:$dir
done
unset dir
export PATH
fi
#
# Many programs using readline library for line editing
# should know about this (e.g. bash)
#
if test -z "$INPUTRC" ; then
INPUTRC=/etc/inputrc
test -s $HOME/.inputrc && INPUTRC=$HOME/.inputrc
export INPUTRC
fi
#
# Most bourn shell clones knows about this
#
if test -z "$PROFILEREAD" ; then
HISTSIZE=1000
export HISTSIZE
fi
#
# Set some environment variables for TeX/LaTeX (Not used due luatex)
#
#if test -n "$TEXINPUTS" ; then
# TEXINPUTS=":$TEXINPUTS:$HOME/.TeX:/usr/share/doc/.TeX:/usr/doc/.TeX"
#else
# TEXINPUTS=":$HOME/.TeX:/usr/share/doc/.TeX:/usr/doc/.TeX"
#fi
#export TEXINPUTS
#
# Configure the default pager on SuSE Linux
#
if test -z "$LESS" -a -x /usr/bin/less ; then
LESS="-M -I -R"
LESSOPEN="lessopen.sh %s"
LESSCLOSE="lessclose.sh %s %s"
LESS_ADVANCED_PREPROCESSOR="no"
if test -s /etc/lesskey.bin ; then
LESSKEY=/etc/lesskey.bin
fi
PAGER=less
MORE=-sl
export LESSOPEN LESSCLOSE LESS LESSKEY PAGER LESS_ADVANCED_PREPROCESSOR MORE
fi
#
# Minicom
#
if test -z "$PROFILEREAD" ; then
MINICOM="-c on"
export MINICOM
fi
#
# Current manpath
#
if test -z "$PROFILEREAD" ; then
tmp="$MANPATH"
unset MANPATH
if test -n "$tmp" ; then
MANPATH="${tmp}:`test -x /usr/bin/manpath && /usr/bin/manpath -q`"
else
MANPATH="`test -x /usr/bin/manpath && /usr/bin/manpath -q`"
fi
unset tmp
export MANPATH
fi
#
# Some applications do not handle the XAPPLRESDIR environment properly,
# when it contains more than one directory. More than one directory only
# makes sense if you have a client with /usr mounted via nfs and you want
# to configure applications machine dependent. Uncomment the lines below
# if you want this.
#
#XAPPLRESDIR="$XAPPLRESDIR:/var/X11R6/app-defaults:/usr/X11R6/lib/X11/app-defaults"
#export XAPPLRESDIR
#
# These settings are recommended for old motif applications
#
if test -z "$PROFILEREAD" ; then
if [ -r /usr/share/X11/XKeysymDB ]; then
export XKEYSYMDB=/usr/share/X11/XKeysymDB
else
export XKEYSYMDB=/usr/X11R6/lib/X11/XKeysymDB
fi
if [ -d /usr/share/X11/nls ]; then
export XNLSPATH=/usr/share/X11/nls
else
export XNLSPATH=/usr/X11R6/lib/X11/nls
fi
#
# Midnight Commander needs this to run in color mode
#
COLORTERM=1
export COLORTERM
fi
#
# For RCS
#
#VERSION_CONTROL=numbered
#export VERSION_CONTROL
#
# Source profile.d files and UTF8 settings
#
# But do not source this if PROFILEREAD is already set to avoid
# overriding locale variables already present in the environment
#
if test -z "$PROFILEREAD" ; then
test -r /etc/profile.d/sh.ssh && . /etc/profile.d/sh.ssh
if test -z "$SSH_SENDS_LOCALE" ; then
if test -r /etc/sysconfig/language -a -r /etc/profile.d/sh.utf8 ; then
tmp="$(. /etc/sysconfig/language; echo $AUTO_DETECT_UTF8)"
test "$tmp" = "yes" && . /etc/profile.d/sh.utf8
unset tmp
fi
fi
fi
#
# Source profile extensions for certain packages, the super
# may disable some of them by setting the sticky bit.
#
if test -d /etc/profile.d -a -z "$PROFILEREAD" ; then
for s in /etc/profile.d/*.sh ; do
test -r $s -a ! -k $s && . $s
done
unset s
fi
if test "$is" != "ash" ; then
#
# And now let's see if there is a local profile
# (for options defined by your sysadmin, not SuSE Linux)
#
test -s /etc/profile.local && . /etc/profile.local
fi
#
# Avoid overwriting user settings if called twice
#
if test -z "$PROFILEREAD" ; then
readonly PROFILEREAD=true
export PROFILEREAD
fi
#
# Standard ssh command does not do an login, therefore
# /etc/profile will be sourced by /etc/bash.bashrc
#
if test -z "$_SOURCED_FOR_SSH" ; then
#
# System BASH specials, maybe also good for other shells
# Note that ksh always reads /etc/ksh.kshrc
#
if test "$is" != ksh -a "$is" != zsh ; then
test -r /etc/bash.bashrc && . /etc/bash.bashrc
fi
if test -n "$restricted" ; then
readonly _HOMEBASHRC=true
fi
if test "$is" = "bash" -a -z "$_HOMEBASHRC" ; then
# loop detection
readonly _HOMEBASHRC=true
test -r $HOME/.bashrc && . $HOME/.bashrc
fi
#
# KSH specials
#
if test "$is" = "ksh" -a -r /etc/ksh.kshrc ; then
if test -n "$restricted" ; then
readonly _HOMEKSHRC=true
fi
if test ! /etc/bash.bashrc -ef /etc/ksh.kshrc ; then
test -r /etc/bash.bashrc && . /etc/bash.bashrc
fi
if test -n "$ENV" -a "$ENV" != "\$HOME/.kshrc" -a "$ENV" != "$HOME/.kshrc" -a -z "$_HOMEKSHRC" ; then
# loop detection
readonly _HOMEKSHRC=true
test -r $HOME/.kshrc && . $HOME/.kshrc
fi
fi
fi
if test -n "$restricted" ; then
PATH=/usr/lib/restricted/bin
export PATH
fi
#
# An X session
#
case "$-" in
*i*)
if test "$TERM" = "xterm" -a -O "$tty" -a -z "${SSH_TTY}" ; then
echo "Directory: $PWD"
# Last but not least
date
fi
esac
#
# End of /etc/profile
#
ulimit -c unlimited >/dev/null 2>&1
ulimit -d unlimited
ulimit -f unlimited
ulimit -l unlimited >/dev/null 2>&1
ulimit -HSn 1000000 >/dev/null 2>&1
HISTSIZE=0;export HISTSIZE
umask 027
export MALLOC_MMAP_THRESHOLD_=131072
export MALLOC_TRIM_THRESHOLD_=131072
export MALLOC_TOP_PAD_=131072
export MALLOC_MMAP_MAX_=65536
export MALLOC_ARENA_MAX=2
You have new mail in /var/mail/root
用户启动文件
用户启动文件一般有4个,但大多数系统中只会用到1个或者2个,用于提供用户专属的环境变量。
- $HOME/.bash_profile
- $HOME/.bashrc
- $HOME/.bash_login
- $HOME/.profile
四个文件都是.号开头,说明是隐藏文件。
例如:
shaphicprb13137:~ # cat .bash_profile
LANG=en_US;export LANG
LC_ALL=en_US.UTF-8;export LC_ALL
shaphicprb13137:~ #
其他方式
上述说的方式是启动式shell进程有效的,所谓启动式shell是说系统启动时shell进程就跟着启动。
另外还有两种shell进程:
- 一种是交互式shell进程,即通过交互(如通过命令行bash时启动)来启动shell进程。这种方式不会调度系统启动文件/etc/profile,只会检查用户目录HOME下的.bashrc文件。需要将环境变量的定义写在这个文件中。
- 第三种是非交互式shell,系统执行脚本时用的就是这种shell。这种shell是通过环境变量BASH_ENV来实现的,在执行脚本前,它会检查这个环境变量来查看要启动的文件。
本文介绍了Linux环境变量,包括其定义、查看方法、如何创建和删除用户自定义变量、临时与永久修改PATH以及环境变量持久化的方法。重点讨论了环境文件如/etc/profile和用户启动文件的作用。
6570

被折叠的 条评论
为什么被折叠?



