zoukankan      html  css  js  c++  java
  • Ubuntu环境变量设置

    在配置Ubuntu里面的JDK环境变量时,从网上找到的资料各异,在不同的文件里面配置,如/etc/environment、/etc/profile,环境变量设置都是可以的。但是难免会有其它的疑问,不同文件配置有什么不同呢?在网上找了好多资料看了一下,现在将体会以的一点东西列出来。 

    为了准确性,直接找英文的资料,找到了一个Ubuntu官方的网站关于系统环境变量的一些说明https://help.ubuntu.com/community/EnvironmentVariables。看里面的内容,发现网上的不少应该是直接翻译的这个网页里面的一些内容,只是没有注明而已。 

    这个网页里面列出了很多有环境变量有关的文件,主要有~/.profile、~/.bash_logout、~/.bash_login、~/.bash_profile、/etc/environment、/etc/profile、/etc/bash.bash.rc。既然知道有这些文件,那就每个文件里面的内容都看看。

     1. 用户配置文件

    列出用户目录文件所有文件,la (ls -a的快捷键)

    里面和环境变量相关的文件有:

    • .bash_logout 
    • .bashrc
    • .profile
    分别查看一下各个文件里面的内容
    1).bash_logout 用户退出登录执行的操作
    # ~/.bash_logout: executed by bash(1) when login shell exits.
    # when leaving the console clear the screen to increase privacy
    2).bashrc 非登录用户shell一些变量的设置
    # ~/.bashrc: executed by bash(1) for non-login shells.
    3).profile 登录用户shell一些变量的设置
    # ~/.profile: executed by the command interpreter for login shells.
    # This file is not read by bash(1), if ~/.bash_profile or ~/.bash_login exists.     如果~/.bash_profile或~/.bash_login存在时,不会使用这个文件的设置
    # the default umask is set in /etc/profile
    # include .bashrc if it exists 该文件里面会包含.bashrc文件,如果文件存在

    在查看这几个文件里面的内容时,里面有提到bash的一些相关资料,安装一个deb包之后会有bash.pdf 和bashref.pdf文件,官方文档,介绍很清楚,但是都是英文的看起来很费劲,下面附录里面的中文版链接。 


    2. 系统配置文件
    1  /etc/environment文件
    这个文件里面设置PATH环境变量

    2  /etc/profile
    系统级环境变量设置,该文件里面的设置是全局的,包含有/etc/bash.bash.rc文件里面的设置,可以在该文件里面覆写/etc/bash.bash.rc里面的设置
    # /etc/profile: system-wide .profile file for the Bourne shell (sh(1))
    # and Bourne compatible shells (bash(1), ksh(1), ash(1), ...).
    # The file bash.bashrc already sets the default PS1.

    3 /etc/bash.bash.rc
    该文件里面的设置是全局的
    # System-wide .bashrc file for interactive bash(1) shells.
    # To enable the settings / commands in this file for login shells as well,
    # this file has to be sourced in /etc/profile.

    3. 设置系统环境变量
    在官方文档EnvironmentVariables里面已经讲得比较清楚,具体的看Persistent environment variables这一段,不推荐在

    ~/.profile、~/.bash_profile 、 ~/.bash_login、~/.bashrc

    /etc/profile、/etc/bash.bashrc

    这几个文件里面进行修改,所以最好在/etc/environment 这个文件里面时行环境变量的修改。


    补充说明:
    bash的启动(Invocation)
    当bash是作为交互的登录shell启动的,或者是一个非交互的shell但是指定了--login选项,它首先读取并执行/etc/profile中的命令,如果那个文件存在。读取那个文件之后,它以如下的顺序查找:~/.bash_profile → ~/.bash_login → ~/.profile (包含有~/.bashrc),从存在并且可读的第一个文件中读取并执行其中的命令。
    当一个登录shell退出时,bash读取并执行文件~/.bash_logout中的命令,当其存在时。
    当一个交互的shell但是不是登录shell启动时,bash从文件~/.bashrc中读取并执行命令,当文件存在时。


    附录:

    官方环境变量说明:https://help.ubuntu.com/community/EnvironmentVariables

            Bash.pdf中文版:http://pan.baidu.com/share/link?shareid=284279293&uk=3457029223

            bashref.pdf中文版:http://pan.baidu.com/share/link?shareid=293108901&uk=3457029223

  • 相关阅读:
    maven-仓库
    maven-坐标与依赖
    maven-认识
    mysql-介绍
    redis-主从复制
    UVA
    UVA
    UVA
    UVA
    [ Java学习 ] 查阅资料整理 004
  • 原文地址:https://www.cnblogs.com/adm1989/p/3196354.html
Copyright © 2011-2022 走看看