linux環(huán)境變量

linux登陸環(huán)境變量初始化順序

/etc/profile-->/.bash_profile-->/.bash_login-->~/.profile

參考:

   A  login  shell  is  one whose first character of argument
   zero is a -, or one started with the --login option.

   An interactive shell is  one  started  without  non-option
   arguments  and  without the -c option whose standard input
   and error are both connected to terminals  (as  determined
   by  isatty(3)), or one started with the -i option.  PS1 is
   set and $- includes i if bash is interactive,  allowing  a
   shell script or a startup file to test this state.

   The  following  paragraphs  describe how bash executes its
   startup files.  If any of the files exist  but  cannot  be
   read,  bash reports an error.  Tildes are expanded in file
   names as described below  under  Tilde  Expansion  in  the
   EXPANSION section.

   When  bash is invoked as an interactive login shell, or as
   a non-interactive shell with the --login option, it  first
   reads and executes commands from the file /etc/profile, if
   that file exists.  After reading that file, it  looks  for
   ~/.bash_profile,  ~/.bash_login,  and  ~/.profile, in that
   order, and reads and executes commands from the first  one
   that  exists  and is readable.  The --noprofile option may
   be used when the shell is started to inhibit  this  behav-
   ior.

   When a login shell exits, bash reads and executes commands
   from the file ~/.bash_logout, if it exists.

   When an interactive shell that is not  a  login  shell  is
   started,  bash reads and executes commands from ~/.bashrc,
   if that file exists.  This may be inhibited by  using  the
   --norc  option.   The --rcfile file option will force bash
   to  read  and  execute  commands  from  file  instead   of
   ~/.bashrc.

   When  bash  is  started  non-interactively, to run a shell
   script, for example, it looks for the variable BASH_ENV in
   the  environment,  expands  its value if it appears there,
   and uses the expanded value as the name of a file to  read
   and  execute.   Bash  behaves  as if the following command
   were executed:
          if [ -n "$BASH_ENV" ]; then . "$BASH_ENV"; fi
   but the value of the PATH variable is not used  to  search
   for the file name.

Part two

用戶登錄到Linux系統(tǒng)后,系統(tǒng)將啟動一個用戶shell。在這個shell中,可以使用shell命令或聲明變量,也可以創(chuàng)建并運行shell腳本程序。運行shell腳本程序時,系統(tǒng)將創(chuàng)建一個子shell。此時,系統(tǒng)中將有兩個shell,一個是登錄時系統(tǒng)啟動的shell,另一個是系統(tǒng)為運行腳本程序創(chuàng)建的shell。當(dāng)一個腳本程序運行完畢,它的腳本shell將終止,可以返回到執(zhí)行該腳本之前的shell。
我們可以在腳本中通過命令export將變量輸出到任何子shell;而通過source命令(.命令),可以執(zhí)行shell腳本,并同時將腳本中的變量輸出到當(dāng)前shell。

Tips

  1. 有時,我們在crontab上定時調(diào)用在腳本,但是腳本執(zhí)行一些命令沒有成功。這時,我們可以將命令的調(diào)用方式都改成用絕對路徑的方式;還有另一種方法,在腳本里加上如下信息,這樣腳本調(diào)用命令也能正常執(zhí)行了。

export PATH=$PATH:/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin

  1. 當(dāng)我們更改了環(huán)境變量,需要重新登陸shell才生效。其實也可以通過命令 source 更改的文件 來使其生效。
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

推薦閱讀更多精彩內(nèi)容