linux中的环境变量/etc/profile /etc/bashrc ~/.bash_profile ~/.bashrc


来源:https://blog.csdn.net/zzhongcy/article/details/108663751

  1. /etc/profile
  2. ============
  3. 此文件为系统的每个用户设置环境信息,当用户第一次登录时,该文件被执行.
  4. 并从/etc/profile.d目录的配置文件中搜集shell的设置.
  5. ===========
  6. /etc/bashrc
  7. ===========
  8. 为每一个运行bash shell的用户执行此文件.当bash shell被打开时,该文件被读取.
  9. ===============
  10. ~/.bash_profile
  11. ===============
  12. 每个用户都可使用该文件输入专用于自己使用的shell信息,当用户登录时,该
  13. 文件仅仅执行一次!默认情况下,他设置一些环境变量,执行用户的.bashrc文件.
  14. =========
  15. ~/.bashrc
  16. =========
  17. 该文件包含专用于你的bash shell的bash信息,当登录时以及每次打开新的shell时,该文件被读取.
  18. ==========
  19. ~/.profile
  20. ==========
  21. 在Debian中使用.profile文件代 替.bash_profile文件
  22. .profile(由Bourne Shell和Korn Shell使用)和.login(由C Shell使用)两个文件是.bash_profile的同义词,目的是为了兼容其它Shell。在Debian中使用.profile文件代 替.bash_profile文件。
  23. ==============
  24. ~/.bash_logout
  25. ==============当每次退出系统(退出bash shell)时,执行该文件.

/etc/profile


此文件为系统的每个用户设置环境信息,当用户第一次登录时,该文件被执行.并从/etc/profile.d目录的配置文件中搜集shell的设置.

英文描述为:

  1. # /etc/profile
  2. # System wide environment and startup programs, for login setup
  3. # Functions and aliases go in /etc/bashrc
  4. # It's NOT a good idea to change this file unless you know what you
  5. # are doing. It's much better to create a custom.sh shell script in
  6. # /etc/profile.d/ to make custom changes to your environment, as this
  7. # will prevent the need for merging in future updates.

所以如果你有对/etc/profile有修改的话必须得重启你的修改才会生效,此修改对每个用户都生效。

/etc/bashrc

为每一个运行bash shell的用户执行此文件.当bash shell被打开时,该文件被读取,每次用户打开一个终端时,即执行此文件

英文描述为:

  1. # /etc/bashrc
  2. # System wide functions and aliases
  3. # Environment stuff goes in /etc/profile
  4. # It's NOT a good idea to change this file unless you know what you
  5. # are doing. It's much better to create a custom.sh shell script in
  6. # /etc/profile.d/ to make custom changes to your environment, as this
  7. # will prevent the need for merging in future updates.

如果你想对所有的使用bash的用户修改某个配置并在以后打开的bash都生效的话可以修改这个文件,修改这个文件不用重启,重新打开一个bash即可生效。

~/.bash_profile


每个用户都可使用该文件输入专用于自己使用的shell信息,当用户登录时,该文件仅仅执行一次!默认情况下,他设置一些环境变量,执行用户的.bashrc文件.

此文件类似于/etc/profile,也是需要需要重启才会生效,/etc/profile对所有用户生效,~/.bash_profile只对当前用户生效。

~/.profile

Linux的Shell种类众多,常见的有:
Bourne Shell(/usr/bin/sh或/bin/sh)、
Bourne Again Shell(/bin/bash)、
C Shell(/usr/bin/csh)、
K Shell(/usr/bin/ksh)、
Shell for Root(/sbin/sh),等等。

不同的Shell语言的语法有所不同,所以不能交换使用。每种Shell都有其特色之处,基本上,掌握其中任何一种 就足够了。在本文中,我们关注的重点是Bash,也就是Bourne Again Shell,由于易用和免费,Bash在日常工作中被广泛使用;同时,Bash也是大多数Linux系统默认的Shell。

在一般情况下,人们并不区分 Bourne Shell和Bourne Again Shell,所以,在下面的文字中,我们可以看到#!/bin/sh,它同样也可以改为#!/bin/bash。

由于这层原因在Debian中使用.profile文件代 替.bash_profile文件以兼容不同的shell

.profile(由Bourne Shell和Korn Shell使用)和.login(由C Shell使用)两个文件是.bash_profile的同义词,目的是为了兼容其它Shell。

~/.bashrc


该文件包含专用于你的bash shell的bash信息,当登录时以及每次打开新的shell时,该文件被读取.(每个用户都有一个.bashrc文件,在用户目录下)

此文件类似于/etc/bashrc,不需要重启生效,重新打开一个bash即可生效, /etc/bashrc对所有用户新打开的bash都生效,但~/.bashrc只对当前用户新打开的bash生效。

~/.bash_logout


当每次退出系统(退出bash shell)时,执行该文件.

另外,/etc/profile中设定的变量(全局)的可以作用于任何用户,而~/.bashrc等中设定的变量(局部)只能继承/etc/profile中的变量,他们是”父子”关系.

~/.bash_profile 是交互式、login 方式进入bash 运行的;
~/.bashrc 是交互式 non-login 方式进入bash 运行的;
通常二者设置大致相同,所以通常前者会调用后者。
设置生效:可以重启生效,也可以使用命令:source

上 面这三个文件是bash shell的用户环境配置文件,位于用户的主目录下。其中.bash_profile是最重要的一个配置文件,它在用户每次登录系统时被读取,里面的所有 命令都会被bash执行。

.bashrc文件会在bash shell调用另一个bash shell时读取,也就是在shell中再键入bash命令启动一个新shell时就会去读该文件。这样可有效分离登录和子shell所需的环境。但一般 来说都会在.bash_profile里调用.bashrc脚本以便统一配置用户环境。

.bash_logout在退出shell时被读取。所以我们可把一些清理工作的命令放到这文件中。

在 /etc目录的bash.bashrc和profile是系统级(全局)的配置文件,当在用户主目录下找不到.bash_profile 和.bashrc\时,就会读取这两个文件。.bash_history是bash shell的历史记录文件,里面记录了你在bash shell中输入的所有命令。可通过HISSIZE环境变量设置在历史记录文件里保存记录的条数。alias l = ‘ls -l’是设置别名的语句,把它放在这些配置文档中就可使我们能用简单的’l’命令,代替’ls -l’命令。

当我们修改了这些配置件后,可用source .bash_profile命令使它修改内容马上生效。

执行顺序


关于登录linux时,/etc/profile、~/.bash_profile等几个文件的执行过程。
如图所示,

**其中~/.bash_profile、~/.bash_login、~/.profile三个文件中往往系统中
往往只存在一个,在不同的发行版中不同,如CentOS和RedHat中?~/.bash_profile,而Debian和Ubunto等系列中往往是~/.profile**

在登录Linux时要执行文件的过程如下:
①在 刚登录Linux时,
首先启动 /etc/profile 文件,
然后再启动用户目录下的 ~/.bash_profile、 ~/.bash_login或 ~/.profile文件中
的其中一个,执行的顺序为:~/.bash_profile、 ~/.bash_login、 ~/.profile
以上两个文件会在用户登录时执行

②下面开始执行用户的bash设置
如果 ~/.bash_profile文件存在的话,一般会以这样的方式执行用户的 ~/.bashrc文件。
在 ~/.bash_profile文件中一般会有下面的代码:

  1. # if running bash
  2. if [ -n "$BASH_VERSION" ]; then
  3. # include .bashrc if it exists
  4. if [ -f "$HOME/.bashrc" ]; then
  5. . "$HOME/.bashrc"
  6. fi
  7. fi

同样~/.bashrc中,一般还会在文件的前面有以下代码,来执行/etc/bashrc

  1. if [ -f /etc/bashrc ] ; then
  2.  . /etc/bashrc
  3. fi

所以,~/.bashrc会调用 /etc/bashrc文件。最后,在退出shell时,还会执行 ~/.bash_logout文件。

**执 行顺序为

  1. /etc/profile
  2. ~/.bash_profile | ~/.bash_login | ~/.profile
  3. ~/.bashrc
  4. /etc/bashrc
  5. ~/.bash_logout

为了验证,我 做了个实验,在/etc/profile,/etc/bashrc,~/.bashrc和~/.bash_profile文件的最后追加同一个变量分别赋 予不同的值,实验结果表明变量最后的值为~/.bash_profile里的值。(4个文件都没有修改其他设置,都是安装系统后的默认值。)
再有就是4个文件都追加一个值到同一个文件,开机后查看该文件内容的顺序为:
/etc/profile
~/.bash_profile
~/.bashrc
/etc/bashrc

作用域


关于各个文件的作用域,在网上找到了以下说明:
(1)/etc/profile: 此文件为系统的每个用户设置环境信息,当用户第一次登录时,该文件被执行. 并从/etc/profile.d目录的配置文件中搜集shell的设置。

(2)/etc/bashrc: 为每一个运行bash shell的用户执行此文件.当bash shell被打开时,该文件被读取。

(3)~/.bash_profile: 每个用户都可使用该文件输入专用于自己使用的shell信息,当用户登录时,该文件仅仅执行一次!默认情况下,他设置一些环境变量,执行用户的.bashrc文件。

(4)~/.bashrc: 该文件包含专用于你的bash shell的bash信息,当登录时以及每次打开新的shell时,该该文件被读取。

(5)~/.bash_logout: 当每次退出系统(退出bash shell)时,执行该文件. 另外,/etc/profile中设定的变量(全局)的可以作用于任何用户,而~/.bashrc等中设定的变量(局部)只能继承/etc /profile中的变量,他们是”父子”关系。

(6)~/.bash_profile 是交互式、login 方式进入 bash 运行的~/.bashrc 是交互式 non-login 方式进入 bash 运行的通常二者设置大致相同,所以通常前者会调用后者。

其他


下面是几个例子:
1. 图形模式登录时,顺序读取:/etc/profile和~/.profile
2. 图形模式登录后,打开终端时,顺序读取:/etc/bash.bashrc和~/.bashrc
3. 文本模式登录时,顺序读取:/etc/bash.bashrc,/etc/profile和~/.bash_profile
4. 从其它用户su到该用户,则分两种情况:
(1)如果带-l参数(或-参数,–login参数),如:su -l username,则bash是lonin的,它将顺序读取以下配置文件:/etc/bash.bashrc,/etc/profile和~ /.bash_profile。
(2)如果没有带-l参数,则bash是non-login的,它将顺序读取:/etc/bash.bashrc和~/.bashrc
5. 注销时,或退出su登录的用户,如果是longin方式,那么bash会读取:~/.bash_logout
6. 执行自定义的shell文件时,若使用“bash -l a.sh”的方式,则bash会读取行:/etc/profile和~/.bash_profile,若使用其它方式,如:bash a.sh, ./a.sh,sh a.sh(这个不属于bash shell),则不会读取上面的任何文件。

来源:https://blog.csdn.net/weixin_49539546/article/details/123536394

个人总结   /etc/profile  设置全局变量   ~/.bashrc 用户变量(若没有设置则看全局变量)

.bash_profile 文件中内容调用的是 ~/.bashrc文件

什么是交互式shell和非交互式shell,什么是login shell 和non-login shell。

交互式模式:就是shell等待你的输入,并且执行你提交的命令。这种模式被称作交互式是因为shell与用户进行交互。这种模式也是大多数用户非常熟悉的:登录、执行一些命令、签退。当你签退后,shell也终止了。

非交互式模式:在这种模式下,shell不与你进行交互,而是读取存放在文件中的命令,并且执行它们。当它读到文件的结尾,shell也就终止了。

login shell:就是需要输入用户名及密码登录的shell,如开机shell登陆、ssh登陆、su - username(从root切换到其他用户时无需密码)。

non-login shell:无需输入密码,如bash、su username、图形界面打开bash。

bashrc用于交互式non-login shell,而profile用于交互式login shell。系统中存在许多bashrc和profile文件 

交互式登录(login)模式:主要用来设置一些系统变量

/etc/profile:(针对所有用户)此文件为系统的每个用户设置环境信息,当用户第一次登录时,该文件被执行并从/etc/profile.d目录的配置文件中搜集shell的设置。

~/.profile:(针对个人)若bash是以login方式执行时,读取~/.bash_profile,若它不存在,则读取~/.bash_login,若前两者不存在,读取~/.profile.另外,图形模式登录时,此文件将被读取,即使存在~/.bash_profile和~/.bash_login。

~/.bash_profile:每个用户都可使用该文件输入专用于自己使用的shell信息,当用户登录时,该文件仅仅执行一次!默认情况下,他设置一些环境变量,执行用户的.bashrc文件。 

~/.bash_login:若bash是以login方式执行时,读取~/.bash_profile,若它不存在,则读取~/.bash_login,若前两者不存在,读取~/.profile。

交互式非登录(non login)模式:主要用来保存一些bash的设置

/etc/bashrc:为每一个运行bash shell的用户执行此文件.当bash shell被打开时,该文件被读取。 

 ~/.bashrc:该文件包含专用于个人的的bash shell的bash信息,当登录时以及每次打开新的shell时,该该文件被读取。

~/.bash_logout:当每次退出系统(退出bash shell)时,执行该文件。也就是说,在文本模式注销时,此文件会被读取,图形模式注销时,此文件不会被读。

 
下面是在本机的几个例子: 

首先需要明确的是,读取~/.bash_profile,该文件便会去读取~/.bashrc,而~/.bashrc又会去读取/etc/bashrc。所以只要读取~/.bash_profile,便会同时读取~/.bashrc与/etc/bashrc。

1. 图形模式登录时,顺序读取:/etc/profile和~/.profile。

2. 图形模式登录后,打开终端时,顺序读取:~/.bashrc和/etc/bashrc。

3. 文本模式登录时,顺序读取:/etc/profile,~/.bash_profile,~/.bashrc和/etc/bashrc。 

4. 从其它用户su到该用户,则分两种情况:    

   (1)如果带-l参数(或-参数,--login参数),如:su -l username,则bash是lonin的,它将顺序读取以下配置文件:/etc/profile,~/.bash_profile,~/.bashrc和/etc/bashrc。    

   (2)如果没有带-l参数,则bash是non-login的,它将顺序读取:~/.bashrc和/etc/bashrc并从父进程继承其环境变量。

5. 注销时,或退出su登录的用户,如果是longin方式,那么bash会读取:~/.bash_logout

6. 执行自定义的shell文件时,若使用“bash -l a.sh”的方式,则bash会读取行:/etc/profile,~/.bash_profile,~/.bashrc和/etc/bashrc,用sh -l a.sh调用bash,它将会执行/etc/profile以及 ~/.profile。若使用其它方式,如:bash a.sh, ./a.sh,sh a.sh,则不会读取上面的任何文件,只能从其父进程处继承环境变量,像alias等就无法继承使用了。

7. 上面的例子凡是读取到~/.bash_profile的,若该文件不存在,则读取~/.bash_login,若前两者不存在,读取~/.profile。 

来源:https://blog.csdn.net/liujinwei2005/article/details/115602991

bash的startup文件

Linux shell是用户与Linux系统进行交互的媒介,而bash作为目前Linux系统中最常用的shell,它支持的startup文件也并不单一,甚至容易让人感到费解。本文以CentOS7系统为例,对bash的startup文件进行一些必要的梳理和总结。

根据bash手册上的描述:

        /etc/profile
        The systemwide initialization file, executed for login shells
        /etc/bash.bash_logout
        The systemwide login shell cleanup file, executed when a login shell exits
        ~/.bash_profile
        The personal initialization file, executed for login shells
        ~/.bashrc
        The individual per-interactive-shell startup file
        ~/.bash_logout
        The individual login shell cleanup file, executed when a login shell exits

此外,bash还支持~/.bash_login和~/.profile文件,作为对其他shell的兼容,它们与~/.bash_profile文件的作用是相同的。

备注:Debian系统会使用~/.profile文件取代~/.bash_profile文件,因此在相关细节上,会与CentOS略有不同。
“profile”与“rc”系列

通过名字的不同,我们可以直观地将startup文件分为“profile”与“rc”两个系列,其实他们的功能都很类似,但是使用的场景不同,这也是大家最容易忽略的地方。

所谓的不同场景,其实就是shell的运行模式。我们知道运行中的bash有“交互”和“登陆”两种属性,而执行“profile”系列还是“rc”系列,就与shell的这两个属性有关。

关于bash的运行模式,请参见我的另一篇博客:
《关于“交互式-非交互式”与“登录-非登陆”shell的总结》

原理上讲,“登陆shell”启动时会加载“profile”系列的startup文件,而“交互式非登陆shell”启动时会加载“rc”系列的startup文件。
“profile”系列的执行场景

根据bash手册上的描述:

    When bash is invoked as an interactive login shell, or as a non-interactive shell with the --login option, it first reads and executes commands from the file /etc/profile, if that file exists. After reading that file, it looks for ~/.bash_profile, ~/.bash_login, and ~/.profile, in that order, and reads and executes commands from the first one that exists and is readable. The --noprofile option may be used when the shell is started to inhibit this behavior.
    When a login shell exits, bash reads and executes commands from the files ~/.bash_logout and /etc/bash.bash_logout, if the files exists.

“profile”系列的代表文件为~/.bash_profile,它用于“登录shell”的环境加载,这个“登录shell”既可以是“交互式”的,也可以是“非交互式”的。

通过--noprofile选项可以阻止系统加载“profile”系列的startup文件。
交互式登陆shell

对于交互式的登陆shell而言,CentOS规定了startup文件的加载顺序如下:

登陆过程:
1. 读取并执行/etc/profile文件;
2. 读取并执行~/.bash_profile文件;
- 若文件不存在,则读取并执行~/.bash_login文件;
- 若文件不存在,则读取并执行~/.profile文件;

登出过程:
1. 读取并执行~/.bash_logout文件;
2. 读取并执行/etc/bash.bash_logout文件;

为了完成实验,我新建了一些系统默认没有提供的startup文件,例如/etc/bash.bash_logout。然后在每个文件中打印了文件名,并将它们之间的显式调用语句注释掉,例如~/.bash_profile对~/.bashrc的显式调用。

“交互式登陆shell”的实验结果如下:

[root@localhost ~]# su - chen
Last login: Tue Apr 18 17:15:08 CST 2017 from 192.168.161.1 on pts/2
execute /etc/profile
execute ~/.bash_profile
-bash-4.2$ exit
logout
execute ~/.bash_logout
execute /etc/bash.bash_logout
[root@localhost ~]#

我们看到,因为执行了~/.bash_profile文件,所以优先级更低的~/.bash_login和~/.profile文件并没有被执行。
我们可以删除~/.bash_profile和~/.bash_login文件,这样系统就会找到并执行~/.profile文件:

[root@localhost ~]# mv /home/chen/.bash_profile /home/chen/.bash_profile.bak
[root@localhost ~]# mv /home/chen/.bash_login /home/chen/.bash_login.bak
[root@localhost ~]# su - chen
Last login: Tue Apr 18 17:27:21 CST 2017 on pts/1
execute /etc/profile
execute ~/.profile
-bash-4.2$ exit
logout
execute ~/.bash_logout
execute /etc/bash.bash_logout
[root@localhost ~]#

非交互式登陆shell

对于非交互式的登陆shell而言,CentOS规定了startup文件的加载顺序如下:

登陆过程:
1. 读取并执行/etc/profile文件;
2. 读取并执行~/.bash_profile文件;
- 若文件不存在,则读取并执行~/.bash_login文件;
- 若文件不存在,则读取并执行~/.profile文件;

我们注意到,与“交互式登陆shell”相比,“非交互式登陆shell”并没有登出的过程,实验也证实了这一点:

-bash-4.2$ bash --login -c "uname -r"
execute /etc/profile
execute ~/.bash_profile
3.10.0-514.el7.x86_64
-bash-4.2$     # 此时非交互式shell已退出

“rc”系列的执行场景

根据bash手册上的描述:

    When an interactive shell that is not a login shell is started, bash reads and executes commands from ~/.bashrc, if that file exists. This may be inhibited by using the --norc option. The --rcfile file option will force bash to read and execute commands from file instead of ~/.bashrc.

“rc”系列的代表文件为~/.bashrc,它用于“交互式非登录shell”的环境加载。

通过--norc选项可以阻止系统加载“rc”系列的startup文件;通过--rcfile选项可以使用指定的文件替代系统默认的~/.bashrc文件。
交互式非登陆shell

对于交互式的非登陆shell而言,CentOS规定了startup文件的加载顺序如下:
1. 读取并执行~/.bashrc或--rcfile选项指定的文件

这里需要说明,其实“rc”系列startup文件还包括/etc/bashrc。但是系统并不直接调用这个文件,而是通过~/.bashrc文件显式地调用它。

为了完成实验,我在每个startup文件中打印了文件名,并将它们之间的显式调用语句注释掉,例如~/.bashrc对/etc/bashrc的显式调用。

“交互式非登陆shell”的实验结果如下:

[root@localhost ~]# su chen
execute ~/.bashrc
bash-4.2$ exit
exit
[root@localhost ~]#

startup文件的默认调用关系

细心的用户会发现,startup文件的加载并不像上面所述的那样简单。这是因为在CentOS中,startup文件之间还存在着默认的显式调用关系,它们是:
1. ~/.bash_profile显式调用~/.bashrc文件;
2. ~/.bashrc显式调用/etc/bashrc文件;
再看startup文件

分别打开/etc/profile和/etc/bashrc两个文件,我们可以看到:

[root@localhost ~]# head /etc/profile
# /etc/profile

# System wide environment and startup programs, for login setup
# Functions and aliases go in /etc/bashrc

# It's NOT a good idea to change this file unless you know what you
# are doing. It's much better to create a custom.sh shell script in
# /etc/profile.d/ to make custom changes to your environment, as this
# will prevent the need for merging in future updates.

[root@localhost ~]# head /etc/bashrc
# /etc/bashrc

# System wide functions and aliases
# Environment stuff goes in /etc/profile

# It's NOT a good idea to change this file unless you know what you
# are doing. It's much better to create a custom.sh shell script in
# /etc/profile.d/ to make custom changes to your environment, as this
# will prevent the need for merging in future updates.

由此可见,“profile”系列文件的主要目的在于为“登录shell”设置环境变量和启动程序;而“rc”系列文件的主要目的在于设置功能和别名。

顺便提一句,Linux中“rc”是英文“run command”的缩写,表示文件中存放需要执行的命令。其实这也非常符合逻辑,设置功能就要执行shopt命令,而设置别名要执行alias命令。与“rc”系列互补,“profile”系列用来设置环境变量,它不会去调用这两个命令,但却经常需要使用export语句。不信你可以看一看这两个文件。

另外值得一提的是,这两个文件同时提到了一个位置:/etc/profile.d目录。这个目录用于存放个性化配置脚本,你可以把自己需要的全局配置放入以.sh结尾的文件中,系统在执行/etc/profile和/etc/bashrc文件时,都会择机调用它们。这样做最大的好处是便于维护,而且相对更加安全。

这些文件的编写方法,可以参考目录下已有的文件:

[root@localhost ~]# ls /etc/profile.d/*.sh
/etc/profile.d/256term.sh    /etc/profile.d/colorls.sh  /etc/profile.d/less.sh
/etc/profile.d/colorgrep.sh  /etc/profile.d/lang.sh     /etc/profile.d/which2.sh

总结

对于“登录shell”而言,“交互式”执行“登陆”和“登出”相关的“profile”系列startup文件,“非交互式”只执行“登陆”相关的“profile”系列startup文件;对于“非登陆shell”而言,“交互式”执行“rc”系列的startup文件,而“非交互式”执行的配置文件由环境变量BASH_ENV指定。

Linux中startup文件区分全局和个人:全局startup文件放在/etc目录下,用于设置所有用户共同的配置,除非你清楚地知道你在做的事情,否则不要轻易改动它们;个人startup文件放在~目录下,用于设置某个用户的个性化配置。

~/.bash_profile会显式调用~/.bashrc文件,而~/.bashrc又会显式调用/etc/bashrc文件,这是为了让所有交互式界面看起来一样。无论你是从远程登录(登陆shell),还是从图形界面打开终端(非登陆shell),你都拥有相同的提示符,因为环境变量PS1在/etc/bashrc文件中被统一设置过。

下面我来对startup文件进行一个完整的总结:
startup文件    交互登陆    非交互登陆    交互非登陆    非交互非登陆
/etc/profile    直接执行1    直接执行1    -    -
~/.bash_profile    直接执行2    直接执行2    -    -
~/.bash_login    条件执行2    条件执行2    -    -
~/.profile    条件执行2    条件执行2    -    -
~/.bash_logout    直接执行3    不执行    -    -
/etc/bash.bash_logout    直接执行4    不执行    -    -
~/.bashrc    引用执行2.1    引用执行2.1    直接执行1    -
/etc/bashrc    引用执行2.2    引用执行2.2    引用执行1.1    -

备注:
1. “直接执行”表示此文件被系统直接调用,它的执行是无条件的;
2. “条件执行”表示此文件被系统调用是有先决条件的(没有优先级更高的文件可用);
3. “引用执行”表示此文件不是被系统直接调用的,而是被其他文件显式调用的;
4. 后面的数字表示文件被调用的顺序,数字越大调用越靠后;
5. “非交互非登陆”shell的配置文件可以由BASH_ENV环境变量指定;

最后我想说的是,知道startup文件何时被执行并不是关键,关键是要理解自己的情况应该去修改哪个startup文件。

如果你想对bash的功能进行设置或者是定义一些别名,推荐你修改~/.bashrc文件,这样无论你以何种方式打开shell,你的配置都会生效。而如果你要更改一些环境变量,推荐你修改~/.bash_profile文件,因为考虑到shell的继承特性,这些更改确实只应该被执行一次(而不是多次)。针对所有用户进行全局设置,推荐你在/etc/profile.d目录下添加以.sh结尾的文件,而不是去修改全局startup文件。

来源:https://blog.csdn.net/sinoyang/article/details/124206564

/etc/profile、/etc/bashrc、~/.bashrc、.bash_profile这四个文件在用户登录时候的执行顺序,这个网上各种资料说法不一,我们自己来探究一下它执行的先后顺序。思路是这样的,在这四个文件的最后一行加一行日志,然后进行重启,然后查看打印日志的先后顺序。具体操作如下:

1、增加打印日志

vim /etc/profile

 # 在/etc/profile最后一行加入下面一行
 echo '/etc/profile is readed!' >> ~/my.log

vim /etc/bashrc

 # 在/etc/bashrc最后一行加入下面一行
 echo '/etc/bashrc is readed!' >> ~/my.log

vim ~/.bashrc

 # 在~/.bashrc最后一行加入下面一行
 echo '~/.bashrc is readed!' >> ~/my.log

vim ~/.bash_profile

 # 在~/.bash_profile最后一行加入下面一行
 echo '~/.bash_profile is readed!' >> ~/my.log

2、重启,然后“cat~/my.log”,内容如下:

3、结论

执行的顺序是:/etc/profile、/etc/bashrc、~/.bashrc、~/.bash_profile,至少在我用的CentOS7中是这样的一个顺序。

相关