Linux中profile、bashrc、~/.bash_profile、~/.bashrc、~/.bash_profile之间的区别和联系以及执行顺序

来源:互联网 发布:ap100编程教程 编辑:程序博客网 时间:2024/05/24 15:42

区别


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

/etc/profile


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

英文描述为:

# /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.
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9

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

/etc/bashrc

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

英文描述为:

# /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.
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9

如果你想对所有的使用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文件中一般会有下面的代码:

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

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

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

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

**执 行顺序为

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

为了验证,我 做了个实验,在/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),则不会读取上面的任何文件。

(function () {('pre.prettyprint code').each(function () { var lines = (this).text().split(\n).length;varnumbering = $('
    ').addClass('pre-numbering').hide(); (this).addClass(hasnumbering).parent().append(numbering); for (i = 1; i
    阅读全文
    0 0