zoukankan      html  css  js  c++  java
  • Linux 环境变量详解

    1. $HOME变量从哪里获得的?

    HOME 本身是 shell 来设置的,你可以自己手工设置(例如使用 bash 的 export),也可以在 shell 的初始化文件中设置(例如 bash 的 .bash_profile 或者 .profile)。但是,除非你知道自己在做什么,还是不要修改它的好。

    既然HOME本身是shell来设置的,那么shell从哪里读到这个配置呢?答案是/etc/passwd,例如:

    Jack:x:501:501:Jack:/home/Jack:/bin/bash

    当用户登录时密码验证后,读取该信息。

    2. bash_profile与bashrc的区别,login-shell与non login-shell, 交互式shell。

     /etc/profile此文件为系统的所有用户设置全局环境变量只要你登录了Linux系统,bash就会执行/etc/profile文件中的命令。profile文件会逐一访问位于/etc/profile.d目录的每一个文件,它为linux系统提供了一个集中存放用户登录时需要执行的应用专属的启动文件的地方。

    ~/.bash_profile每个用户都可使用该文件输入专用于自己使用的shell初始化信息,当用户登录时,该文件被执行。 一般设置让它调用用户的~/.bashrc文件.

    上面这两个文件会在以login-shell形式启动bash shell时执行。

     /etc/bashrc:只会在每次有新的交互式shell启动时,该文件才会运行,也就是在~/.bashrc脚本中被调用,默认文件中会设置一些环境变量,但是它并没有执行export命令让他们成为全局的。

    ~/.bashrc该文件包含专用于你的bash shell的bash信息,每次以non login-shell模式即交互式shell启动时,该文件被读取.

    [root@win Jack]# cat .bash_profile
    # .bash_profile
    
    # Get the aliases and functions
    if [ -f ~/.bashrc ]; then
        . ~/.bashrc
    fi
    
    # User specific environment and startup programs
    
    PATH=$PATH:$HOME/bin
    
    export PATH
    [root@win Jack]# cat .bashrc
    # .bashrc
    
    # Source global definitions
    if [ -f /etc/bashrc ]; then
        . /etc/bashrc
    fi
    
    # User specific aliases and functions

    那什么是login-shell?什么是nonlogin-shell交互式shell呢?这个问题刚开始很让我困扰。因为我平时都是直接登录到linux图形界面,打开shell的方面都是使用terminal终端打开的。

    这里作出解释。

    login-shell:就是通过非图形界面进行登录,例如Ctrl+Alt+F[ 2-7 ]切换到tty1~tty6登入时, 需要输入用户名和密码,此时取得的bash就称为login shell

    这个过程会运行以下文件:

    --------> /etc/profile -----------> $HOME/.bash_profile   如果有$HOME/.bash_login 和 $HOME/.profile 文件也会运行 (我系统中没有这两个文件)

    根据我的.bash_profile文件的设置,在.bash_profile文件中会调用  $HOME/.bashrc --------------> /etc/bashrc

    也就是说,在我的机器中,用login-shell启动shell,文件的执行顺序是这样: /etc/profile ----------> $HOME/.bash_profile -----------> $HOME/.bashrc ----------> /etc/bashrc

    non login-shell交互式shell,就是我们最常用,需要注意的是interactive non-login shell从其父进程上继承过来环境变量。

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

          (3)通过su命令切换用户获取的bash也是non-login shell。

    如果bash是交互式non login-shell启动的,它不会去访问/etc/profile文件,而是去用户的HOME目录检查.bashrc时候存在。过程:

    -----------> $HOME/.bashrc -----------> /etc/bashrc

    由于non login-shell不是从非图形界面直接登录的,所以这个进程一定有父进程,而父进程某个父进程为登录进程,运行过/etc/profile和.bash_profile文件,因为/etc/profile中通过export设置的是全局的环境变量,子进程自然得到继承。

    所以在通过交互式shell形式获取bash时,虽然没有去执行/etc/profile文件,但是依然拥有这些环境变量。

    所以non login-shell中实际拥有,/etc/profile$HOME/.bash_profile中的设置的所有全局环境变量(局部环境变量得不到继承,以及$HOME/.bashrc, /etc/bashrc中设置的所有环境变量。

    对此我做出了实验:

    [Jack@win ~]$ cat .bashrc
    # .bashrc
    
    # Source global definitions
    if [ -f /etc/bashrc ]; then
        . /etc/bashrc
    fi
    
    # User specific aliases and functions
    
    echo ".bashrc has been executed."
    [Jack@win ~]$ cat .bash_profile
    # .bash_profile
    
    # Get the aliases and functions
    if [ -f ~/.bashrc ]; then
        . ~/.bashrc
    fi
    
    # User specific environment and startup programs
    
    PATH=$PATH:$HOME/bin
    
    export PATH
    
    echo "bash_profile has been executed" 

    .bashrc.bash_profile文件中分别加上了一行测试语句。测试发现,当我们新起一个交互式shell的时候和su命令切换到oracle用户的时候(我也添加了类似的测试语句),发现都是只执行了.bashrc脚本。

  • 相关阅读:
    设计模式
    设计模式
    设计模式
    设计模式
    【Sublime】许可证 及 相关设置
    【Linux】跳过ubuntu grub2引导,使用Windows引导ubuntu
    【Linux】Windows 7下硬盘安装Ubuntu 14.10图文教程
    【ACM】连连看 hdu1175
    【算法】约瑟夫环 C++源代码
    【Java】配置JAVA的环境变量
  • 原文地址:https://www.cnblogs.com/jackhub/p/3187255.html
Copyright © 2011-2022 走看看