Linux下 /etc/bashrc 和 用户目录下.bashrc的区别

来源:互联网 发布:沈阳浟湙网络怎么样 编辑:程序博客网 时间:2024/04/29 23:37

/etc/bashrc,用户目录下.bashrc有什么区别? 
一个是针对整个系统所有用户的,一个是针对特定用户的./etc/bashrc修改了以后要重启系统才生效,而用户目录下.bashrc修改了以后重新登录就生效 
2。/etc/profile与/etc/bashrc的区别? 
前一个主要用来设置一些系统变量,比如JAVA_HOME等等,后面一个主要用来保存一些bash的设置.  
/etc/profile:此文件为系统的每个用户设置环境信息,当用户第一次登录时,该文件被执行.
并从/etc/profile.d目录的配置文件中搜集shell的设置. 
/etc/bashrc:为每一个运行bash shell的用户执行此文件.当bash shell被打开时,该文件被读取. 
~/.bash_profile:每个用户都可使用该文件输入专用于自己使用的shell信息,当用户登录时,该 
文件仅仅执行一次!默认情况下,他设置一些环境变量,执行用户的.bashrc文件. 
~/.bashrc:该文件包含专用于你的bash shell的bash信息,当登录时以及每次打开新的shell时,该 
该文件被读取. 
~/.bash_logout:当每次退出系统(退出bash shell)时,执行该文件. 
另外,/etc/profile中设定的变量(全局)的可以作用于任何用户,而~/.bashrc等中设定的变量(局部)只能继承/etc/profile中的变量,他们是"父子"关系. 
~/.bash_profile 是交互式、login 方式进入 bash 运行的 
~/.bashrc 是交互式 non-login 方式进入 bash 运行的 
通常二者设置大致相同,所以通常前者会调用后者。 
.bash_profile 文件在通过控制台(或远程登录, 比如ssh)登录(login)系统的时候被执行的shell脚本. 
而 .bashrc 则是在打开虚拟终端的时候才会起作用,比如在GNOME,KDE中执行rxvt.

生效:

(1)在 Linux 管理中,常有需要修改根目录下 .bash_profile 文件,更改环境变量的情况,文件修改后,一般的做法是重新登录,或者重新启动机器,不知道大家是怎么做的,反正我以前是用前面的两个办法中的一个,感觉很不方便。现在发现了一个命令: source,在修改好 .bash_profile 文件后,直接运行这个命令如:

    #soure .bash_profile

    就可以直接让环境变量的修改生效了。

(2)另外据发现 "." 也可以使配置好的环境变量生效,同上的等效命令为:

    #. .bash_profile 网管联盟bitsCN@com

    据笔者认为,“.” 命令应该是soure命令的缩写,欢迎大家发表对本文章的看法。

(3)退出当前用户,重新进入,也可以使环境变量生效

/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.

所以如果你有对/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.

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

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

此文件类似于 /etc/profile ,也是需要需要重启才会生效,/etc/profile对所有用户生效, ~/.bash_profile 只对当前用户生效。
~/ .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 运行的;
通常二者设置大致相同,所以通常前者会调用后者。

阅读全文
0 0