申明: 此博是为了学习和做笔记,几乎都是站在巨人的肩膀上的个人总结。
本博客内容参考博主:
http://blog.youkuaiyun.com/twlkyao/article/details/11615663
http://blog.youkuaiyun.com/robertaqi/article/details/5449763
1. 认识变量控制文件。
用户变量:
~/.bashrc; ~/.bash_profile; ~/.profile
#Note: ~/bash_profile 在ubuntu 12.04中就没有。
系统变量:
/etc/profile; /etc/environment; /etc/bash.bashrc
执行顺序(登陆式shell):
Ubuntu: /etc/profile-->/etc/environment-->$HOME/.profile
RedHat: /etc/profile -> (~/.bash_profile | ~/.bash_login | ~/.profile) -> ~/.bashrc -> /etc/bashrc -> ~/.bash_logout
#Note: profile/bash*等都是shell脚本,有时候会调用其他,如/etc/profile 会调用/etc/profile.d/目录下的等
#Note: 不同文件相同变量会被覆盖,类似于重新赋值。
#Note: /etc/environment 与/etc/profile是有区别的, environment 为系统环境,与登陆用户无关。 profile 是用户通用设置,与登陆用户有关。所以系统级的变量一般都会设置在environment里面,如JDK、SDK、Proxy等。
2. 设置变量。
#设置前须知:配置系统级变量可能会让开机失败,需要小心谨慎。
Over view:
一般分为两类,变量名(如 JAVA_HOME)和变量环境(如/usr/bin/)
设置步奏为: 编辑文件(vim 等) --> 保存执行(bash、source)
临时变量:
shell 输入 export JAVA_HOME= <Java home path>
#Notes: 重启shell 会清空这个变量。
用户变量:
变量名: vim ~/.bashrc --> append: export JAVA_HOME=/xxx/xxx/xxx
变量环境: export PATH=$PATH:$JAVA_HOME/bin/
#Notes: PATH 属于系统变量,重新赋值需要加入原来的PATH值,否则就会丢失东西,引起不必要麻烦。
#Notes: bashrc 是shell执行的最后一层,这里设置比较保险。 结果是这个用户打开shell 窗口就会被执行。
系统变量:
变量名: sudo vim /etc/profile --> append: export JAVA_HOME=/xxx/xxx/xxx
or: sudo vim /etc/environment --> JAVA_HOME=/xxx/xxxx/xxx
变量环境: sudo vim /etc/profile --> export PATH=$PATH:$JAVA_HOME/bin/