为何我们登录linux后默认有很多环境变量?
首先我们登录bash有两种方式:login shell和non-login shell;
login shell:取得bash需要完整的登录流程,即/etc/profile(系统整体的设定)和~.bash_profile ~.bash_login ~/.profile(个人设定);
non-login shell:会读取这个~/.bashrc配置文件
以下4点会取得login shell:
登录系统时获得的顶层shell,无论是通过本地终端登录,还是通过网络ssh登录。这种情况下获得的login shell是一个交互式shell。
在终端下使用--login选项调用bash,可以获得一个交互式login shell。
在脚本中使用--login选项调用bash(比如在shell脚本第一行做如下指定:#!/bin/bash --login),此时得到一个非交互式的login shell。
使用"su -"切换到指定用户时,获得此用户的login shell。如果不使用"-",则获得non-login shell。
1. /etc/profile:若想所有用户登录系统时都要改变,就修改此文件,建议不修改。
[root@www ~]# cat -n /etc/profile #共有80多行 1 # /etc/profile 2 3 # System wide environment and startup programs, for login setup 4 # Functions and aliases go in /etc/bashrc 5 6 # It's NOT a good idea to change this file unless you know what you 7 # are doing. It's much better to create a custom.sh shell script in 8 # /etc/profile.d/ to make custom changes to your environment, as this 9 # will prevent the need for merging in future updates. ………… 根据用户设定PATH变量 ………… 有PATH USER LOGNAME MAIL HOSTNAME HISTSIZE HISTCONTROL这些环境变量等信息;
此文件还会调用/etc/profile.d/*.sh,如
for i in /etc/profile.d/*.sh ; do if [ -r "$i" ]; then if [ "${-#*i}" != "$-" ]; then . "$i" else . "$i" >/dev/null 2>&1 fi fi done [root@www ~]# ls -l /etc/profile.d/ 总用量 44 -rw-r--r--. 1 root root 1127 10月 17 2013 colorls.csh -rw-r--r--. 1 root root 1143 10月 17 2013 colorls.sh -rw-r--r--. 1 root root 192 8月 27 2013 glib2.csh -rw-r--r--. 1 root root 192 8月 27 2013 glib2.sh -rw-r--r--. 1 root root 1741 11月 23 2013 lang.csh -rw-r--r--. 1 root root 2706 11月 23 2013 lang.sh -rw-r--r--. 1 root root 122 2月 7 2007 less.csh -rw-r--r--. 1 root root 108 2月 7 2007 less.sh -rw-r--r--. 1 root root 105 12月 22 2016 vim.csh -rw-r--r--. 1 root root 269 12月 22 2016 vim.sh -rw-r--r--. 1 root root 169 5月 20 2009 which2.sh由lang.sh又去调用/etc/sysconfig/i18n来设置语系。
2. 读完/etc/profile后调用完其他文件。就是调用~/.bash_profile ~/.bash_login ~/.profile
只会读取上面三个文件中的其中一个,默认顺序就是以上顺序,
当修改了整体配置文件或个人配置文件,需要使用source命令来读取这些文件来生效。或者使用. 配置文件使配置文件生效
转载于:https://blog.51cto.com/12107790/2176600