zoukankan      html  css  js  c++  java
  • 设置Linux环境变量的方法和区别_Ubuntu/CentOS

    设置 Linux 环境变量可以通过 export 实现,也可以通过修改几个文件来实现,有必要弄清楚这两种方法以及这几个文件的区别。

    通过文件设置 Linux 环境变量

    首先是设置全局环境变量,对所有用户都会生效:

    
    
        etc/profile: 此文件为系统的每个用户设置环境信息。当用户登录时,该文件被执行一次,并从 /etc/profile.d 目录的配置文件中搜集shell 的设置。一般用于设置所有用户使用的全局变量。
        /etc/bashrc: 当 bash shell 被打开时,该文件被读取。也就是说,每次新打开一个终端 shell,该文件就会被读取。
    
    接着是与上述两个文件对应,但只对单个用户生效:
    
        ~/.bash_profile 或 ~/.profile: 只对单个用户生效,当用户登录时该文件仅执行一次。用户可使用该文件添加自己使用的 shell 变量信息。另外在不同的LINUX操作系统下,这个文件可能是不同的,可能是 ~/.bash_profile, ~/.bash_login 或 ~/.profile 其中的一种或几种,如果存在几种的话,那么执行的顺序便是:~/.bash_profile、 ~/.bash_login、 ~/.profile。比如 Ubuntu 系统一般是 ~/.profile 文件。
        ~/.bashrc: 只对单个用户生效,当登录以及每次打开新的 shell 时,该文件被读取。
    
    此外,修改 /etc/environment 这个文件也能实现环境变量的设置。/etc/environment 设置的也是全局变量,从文件本身的作用上来说, /etc/environment 设置的是整个系统的环境,而/etc/profile是设置所有用户的环境。有几点需注意:
    
        系统先读取 etc/profile 再读取 /etc/environment(还是反过来?)
        /etc/environment 中不能包含命令,即直接通过 VAR="..." 的方式设置,不使用 export 。
        使用 source /etc/environment 可以使变量设置在当前窗口立即生效,需注销/重启之后,才能对每个新终端窗口都生效。
    

    修改 Linux 环境变量实例

    以 Ubuntu 为例,修改 ~/.profile 文件:

    vim ~/.profile

    如果该文件存在,则在文件的最后看到如下代码,PATH 变量的值使用冒号(:)隔开的:

        # set PATH so it includes user's private bin if it exists
        if [ -d "$HOME/bin" ] ; then
            PATH="$HOME/bin:$PATH"
        fi

    在最后加上代码 PATH=”$PATH:/usr/local/hadoop/bin”,注意等号(=)两边不要有空格,即:

        # set PATH so it includes user's private bin if it exists
        if [ -d "$HOME/bin" ] ; then
            PATH="$HOME/bin:$PATH"
        fi
        PATH="$PATH:/usr/local/hadoop/bin"

    因为这个文件是在用户登陆是才读取一次的,所以需要重启才会生效(修改 /etc/profile、/etc/environment 也是如此)。但可以使用命令 source ~/.profile 使其立即生效。通过 echo $PATH 可以看到修改后的变量值:

        source ~/.profile
        echo $PATH

    通过 Shell 命令 export 修改 Linux 环境变量

    另一种修改 Linux 环境变量的方式就是通过 Shell 命令 export,注意变量名不要有美元号 $,赋值语句中才需要有

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

    export 方式只对当前终端 Shell 有效

    使用 export 设置的变量,只对当前终端 Shell 有效,也就是说如果新打开一个终端,那这个 export
    设置的变量在新终端中使无法读取到的。适合设置一些临时变量。

    根据变量所需,选择设置方式,例如 JAVA_HOME 这类变量,就适合将其设为为全局变量,可在 /etc/environment 中设置。

    参考

    http://dblab.xmu.edu.cn/blog/linux-environment-variable/

  • 相关阅读:
    【leetcode】1215.Stepping Numbers
    【leetcode】1214.Two Sum BSTs
    【leetcode】1213.Intersection of Three Sorted Arrays
    【leetcode】1210. Minimum Moves to Reach Target with Rotations
    【leetcode】1209. Remove All Adjacent Duplicates in String II
    【leetcode】1208. Get Equal Substrings Within Budget
    【leetcode】1207. Unique Number of Occurrences
    【leetcode】689. Maximum Sum of 3 Non-Overlapping Subarrays
    【leetcode】LCP 3. Programmable Robot
    【leetcode】LCP 1. Guess Numbers
  • 原文地址:https://www.cnblogs.com/bryce1010/p/9386896.html
Copyright © 2011-2022 走看看