Chinaunix

标题: 关于/etc/bashrc和~/.bashrc两个文件的区别 [打印本页]

作者: 274920831    时间: 2016-06-30 14:42
标题: 关于/etc/bashrc和~/.bashrc两个文件的区别
/etc/bashrc和~/.bashrc两个文件有什么区别?
我看到的在一些用户的home目录里面,在~/.bashrc里面的内容有的是这样的:
if [ -f /etc/bashrc ]; then
        . /etc/bashrc
fi

我想问一下/etc/bashrc与~/.bashrc有什么区别?
作者: cjfeii    时间: 2016-06-30 15:54
有区别的:
/etc/bashrc 不止用于你的用户,还可以用于其他用户
~/.bashrc 只用于当前用户
作者: chenyx    时间: 2016-07-05 08:40
~下的是用户的配置文件,/etc下的是所有用户都用的
作者: lrz    时间: 2016-07-06 18:29
我自己的理解是bash启动时先读/etc/bashrc,再读~/.bashrc。不知对不对。
作者: cjfeii    时间: 2016-07-11 09:12
应该是只读~/.bashrc,看一下~/.bashrc中的代码:
  1. # .bashrc

  2. # Source global definitions
  3. if [ -f /etc/bashrc ]; then
  4.         . /etc/bashrc
  5. fi
复制代码
只是在~/.bashrc里读取/etc/bashrc,如果注释掉这段代码,那就应该不读取/etc/bashrc。

回复 4# lrz


   
作者: lrz    时间: 2016-07-13 02:16
cjfeii 发表于 2016-07-11 09:12
应该是只读~/.bashrc,看一下~/.bashrc中的代码:只是在~/.bashrc里读取/etc/bashrc,如果注释掉这段代码, ...


有点奇怪,我查了一下以前我看过的一本书,提到的bash的初始化:
Bourne Again shell的初始化共有8个步骤,初始化文件有/etc/profile、.profile、.bash_profile、.bash_login:
1、bash检查/etc/profile文件是否存在。
2、若存在,shell读取该文件,否则跳过。
3、bash检查主目录下的.bash_profile文件是否存在。
4、若存在,bash读取该文件,否则跳过。
5、bash检查主目录下的.bash_login文件是否存在。
6、若存在,bash读取该文件,否则跳过。
7、bash检查主目录下的.profile文件是否存在。
8、若存在,bash读取该文件,否则跳过。
然后显示提示符“$ ”

没提到bashrc,不过按我以前的理解,linux的程序初始化应该是先读取/etc中的默认设置,再读$HOME的个人设置,人个设置覆盖默认设置完成启动。
刚才看了下我的linux,没/etc/bashrc,只有/etc/bash/bashrc,~/.bashrc没你的那段代码,在/etc/bash/bashrc中加了一句echo "in /etc",再启动个终端,也证实会先读取/etc/bash/bashrc。
你的那段代码有点怪,又用默认的覆盖了个人设置。
in /etc
setterm: 终端 xterm 不支持 --blank
awk函数
system()
system()函数执行一个以表达式给出的命令。返回被执行命令的退出状态。
if (system("mkdir a") != 0)
0lrz@01:20 ~ $

awk是我在~/.bashrc的输出。
作者: cjfeii    时间: 2016-07-13 09:34
你的主机是什么系统?

回复 6# lrz


   
作者: lrz    时间: 2016-07-14 22:38
gentoo

2lrz@22:56 ~ $bash --version
GNU bash,版本 4.3.46(1)-release (x86_64-pc-linux-gnu)
Copyright (C) 2013 Free Software Foundation, Inc.
许可证 GPLv3+: GNU GPL 许可证第三版或者更新版本 <http://gnu.org/licenses/gpl.html>

This is free software; you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.
作者: lrz    时间: 2016-07-14 22:40
gentoo

2lrz@22:56 ~ $bash --version
GNU bash,版本 4.3.46(1)-release (x86_64-pc-linux-gnu)
Copyright (C) 2013 Free Software Foundation, Inc.
许可证 GPLv3+: GNU GPL 许可证第三版或者更新版本 <http://gnu.org/licenses/gpl.html>

This is free software; you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.
作者: lrz    时间: 2016-07-14 22:42
gentoo
2lrz@22:56 ~ $bash --version
GNU bash,版本 4.3.46(1)-release (x86_64-pc-linux-gnu)
作者: action08    时间: 2016-07-18 20:54
/etc/bashrc 用于所有用户
~/.bashrc 只用于当前用户——同时也是优先级别最高的,因为会覆盖




欢迎光临 Chinaunix (http://bbs.chinaunix.net/) Powered by Discuz! X3.2