首页 > 技术文章 > linux环境变量

forest-wow 2017-07-30 15:09 原文

linux环境变量

定义

(1)login shell:取得bash 时需要完整的登入流程,就称为login shell。举例来说,同tty1~tty6登入时,需要输入用户名和密码,此时取得的bash就称为login shell

(2)non-login shell:取得bash介面的方法不需要重复登入的动作。

举两个例子
(i)以X window登入linux后,再以X的图形化介面启动终端机,此时那个终端机并不需要再次的输入用户名和密码,那个bash的环境就称为non-login shell
(ii)在原本的bashr环境中再次下达bash这个指令,同样没有要求输入用户名和密码,那个第二个bash也是non-login shell

Login Shell Sessions&&Nonlogin Shell Session

Login Shell Sessions:

  • /etc/profile : a global configuration script that applies to all users.
  • ~/.bash_profile : a user’s personal startup file.can be used to extend or overide settings in the global configuration script.
  • ~/.bash_login : if ~/.bash_profile is not found,bash attempts to read this script.
  • ~/.profile : if neither ~/.bash_profile nor ~/.bash_login is found,bash attempts to read this file. This is the default in Debian-based distributions,such as Ubuntu.

Nonlogin Shell Sessions:

  • /etc/bash.bashrc a global configuration script that applies to all users.
  • ~/.bashrc a user’s personal startup file. Can be used to extend or override settings in the global configuration script.

.bashrc与.bash_profile区别

  • .bashrc登录以及打开新的shell时,该文件被读取。
  • .bash_profile默认情况之下,它用来设置一些环境变量,执行用户的.bashrc文件。
  • 执行顺序分为:.bash_profile 、bash_login、profile,并且在~/.bash_profile中一般含有以下代码:
  1. if [ -f ~/.bashrc ] ; then 
  2. . ~/.bashrc 
  3. fi 

~/.bashrc中还会有以下代码:

  1. if [ -f /etc/bashrc ] ; then 
  2. . /bashrc 
  3. fi 

所以,~/.bashrc会调用 /etc/bashrc文件。最后,在退出shell时,还会执行 ~/.bash_logout文件。
执行顺序为:/etc/profile -> (~/.bash_profile | ~/.bash_login | ~/.profile) -> ~/.bashrc -> /etc/bashrc -> ~/.bash_logout
注:/etc/profile: 此文件为系统的每个用户设置环境信息,当用户第一次登录时,该文件被执行. 并从/etc/profile.d目录的配置文件中搜集shell的设置。

推荐阅读