linux下的文件/etc/profile、/etc/bashrc、~/.bash_profile、~/.bashrc辨析

1. 介绍

  • /etc/profile

为每个用户设置环境信息,当用户第一次登录时,该文件被执行,并从 /etc/profile.d目录下配置文件中收集 shell 设置。如果你修改了/etc/profile,必须得执行一次source /etc/profile才会使修改生效,且对每个用户都生效。


  • ~/.bash_profile(ubuntu为 ~/.profile)

单个用户都可使用~/.bash_profile输入专用于自己使用的 shell 信息。当用户登录时,该文件仅执行一次。默认情况下,~/.bash_profile设置了一些环境变量。 ~/.bash_profile类似于 /etc/profile,也是需要 source ~/.bash_profile 才会生效。

~/.profile(由Bourne Shell和Korn Shell使用)和.login(由C Shell使用)两个文件是.bash_profile的同义词。

注:/etc/profile 对系统所有用户生效,~/.bash_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。

  • /etc/bashrc(ubuntu为 /etc/bash.bashrc)

为每一个运行 bash shell 的用户执行此文件。当 bash shell 被打开时,/etc/bashrc被读取。如果你想对所有使用 bash shell的用户修改某个配置,并在以后打开的 bash shell都生效的话,可以修改/etc/bashrc文件,修改后不用重启linux或source一下,只需重新打开一个 bash shell即可生效。

  • ~/.bashrc

包含专属于你的 bash shell 信息,当登录时以及每次打开新的 shell 时,该文件被读取。每个用户都有一个 ~/.bashrc文件,在用户目录下。~/.bashrc类似于 /etc/bashrc,不需要重启linux或source一下,只需重新打开一个 bash shell即可生效。/etc/bashrc 对所有用户新打开的 bash 都生效,但 ~/.bashrc 只对当前用户新打开的 bash 生效。

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


2. 区别和联系

  • /etc目录是系统级(全局)的配置文件,当在用户主目录下找不到~/.bash_profile~/.bashrc时,就会读取/etc/profile/etc/bashrc这两个文件。

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

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

  • ~/.bash_history是bash shell的历史记录文件,里面记录了你在bash shell中输入的所有命令。我们可通过环境变量HISSIZE设置历史记录文件里保存记录的条数。


3. 执行顺序

  • 在刚登录Linux时

首先启动 /etc/profile 文件,然后再启动用户目录下的 ~/.bash_profile~/.bash_login~/.profile的其中一个,执行的顺序为:~/.bash_profile, ~/.bash_login,~/.profile

注:其中~/.bash_profile、 ~/.bash_login和 ~/.profile文件往往只存在一个,这与Linux的发行版本有关。centos中为 ~/.bash_profile,ubuntu则为 ~/.profile。

/etc/profile~/.bash_profile文件会在用户登录时执行。

  • 执行某个用户的bash设置

如果存在~/.bash_profile文件的话,在 ~/.bash_profile文件中一般会有下面的代码,执行用户的 ~/.bashrc文件。

# .bash_profile
# Get the aliases and functions
if [ -f ~/.bashrc ]; then
    . ~/.bashrc
fi
# User specific environment and startup programs
PATH=$PATH:$HOME/bin
export PATH

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

# .bashrc
# User specific aliases and functions
alias rm='rm -i'
alias cp='cp -i'
alias mv='mv -i'
# Source global definitions
if [ -f /etc/bashrc ]; then
    . /etc/bashrc
fi

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

  • 总的执行顺序
/etc/profile  ——> ~/.bash_profile或~/.bash_login或~/.profile ——> ~/.bashrc ——> /etc/bashrc ——> ~/.bash_logout

4. 参考文章

https://www.jianshu.com/p/6d32b166f47d

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值