source命令(从 C Shell 而来)是bash shell的内置命令。点命令,就是一个点符号,(从Bourne Shell而来)是source的另一名称。这两个命令都以一个脚本为参数,该脚本将作为当前shell的环境执行,即不会启动一个新的子进程。所有在脚本中设置的变量将成为当前Shell的一部分。
| [root@dev shell]# cat c.sh . ./env.sh source ./profile.sh echo $env; echo $profile; [root@dev shell]# cat env.sh env='test'; [root@dev shell]# cat profile.sh profile="dev"; [root@dev shell]# sh c.sh test dev |
如果想要删除环境变量
| unset var_name |
设置全局环境变量
上文中,可以知道,如果想要在本进程和子进程中使用共同的环境变量。通过source命令去读取同一个环境变量脚本可以实现。这是用户自定义的方案。但很多时候,我们需要读取的全局环境变量并不知道source,所以需要一个默认的环境变量读取文件。
当你登录Linux系统时,bash shell会作为登录shell启动。登录shell会从5个不同的启动文件里读取
/etc/profile $HOME/.bash_profile $HOME/.bashrc $HOME/.bash_login $HOME/.profile/etc/profile
/etc/profile文件是bash shell默认的主启动文件。只要你登录了Linux系统,bash就会执行/etc/profile启动文件的命令。
| [root@dev shell]# cat /etc/profile # /etc/profile # System wide environment and startup programs, for login setup # Functions and aliases go in /etc/bashrc # It's NOT a good idea to change this file unless you know what you # are doing. It's much better to create a custom.sh shell script in # /etc/profile.d/ to make custom changes to your environment, as this # will prevent the need for merging in future updates. pathmunge () { case ":${PATH}:" in *:"$1":*) ;; *) if [ "$2" = "after" ] ; then PATH=$PATH:$1 else PATH=$1:$PATH fi esac } if [ -x /usr/bin/id ]; then if [ -z "$EUID" ]; then # ksh workaround EUID=`id -u` UID=`id -ru` fi USER="`id -un`" LOGNAME=$USER MAIL="/var/spool/mail/$USER" fi # Path manipulation if [ "$EUID" = "0" ]; then pathmunge /sbin pathmunge /usr/sbin pathmunge /usr/local/sbin else pathmunge /usr/local/sbin after pathmunge /usr/sbin after pathmunge /sbin after fi HOSTNAME=`/bin/hostname 2>/dev/null` HISTSIZE=1000 if [ "$HISTCONTROL" = "ignorespace" ] ; then export HISTCONTROL=ignoreboth else export HISTCONTROL=ignoredups fi export PATH USER LOGNAME MAIL HOSTNAME HISTSIZE HISTCONTROL # By default, we want umask to get set. This sets it for login shell # Current threshold for system reserved uid/gids is 200 # You could check uidgid reservation validity in # /usr/share/doc/setup-*/uidgid file if [ $UID -gt 199 ] && [ "`id -gn`" = "`id -un`" ]; then umask 002 else umask 022 fi for i in /etc/profile.d/*.sh ; do if [ -r "$i" ]; then if [ "${-#*i}" != "$-" ]; then . "$i" else . "$i" >/dev/null 2>&1 fi fi done unset i unset -f pathmunge |








