【centos中etcprofileetcbashrc等文件的区别】centos中/etc/profile,/etc/bashrc等文件的区别与作用

更新时间:2019-11-18    来源:CentOS    手机版     字体:

【www.bbyears.com--CentOS】


当我们在做一些与bash相关的操作时,比如设置别名、登录启动项等,多多少少都会与下面几个文件打交道,用的时候一查,然后又忘了。好记性不如烂笔头,下面老高就帮你理一理这些文件到底是干啥的。

/etc/profile
/etc/bashrc
~/.bash_profile
~/.bashrc

•文件说明:

◦/ect/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 中的变量,他们是"父子"关系.


man bash ----- INVOCATION

以CENTOS为例,其他系统可能略有区别。

登录shell

A login shell is one whose first character of argument zero is a -, or one started with the --login option.
老高注解:

判断一个shell是不是登录shell,只要你是su -, bash --login这样进入的就行,如果你进入一个terminal而不需要登录,即输入用户名密码,那么他就是一个 non-login shell。比如你在MAC下打开终端,是不需要登录过程的所以是non-login shell。

还有一个方法,echo $0,如果是登录shell,则会返回-[bash name],否则返回[bash name]。

交互式Shell

An interactive shell is one started without non-option arguments and without the -c option whose standard input and error are both connected to terminals (as determined by isatty(3)), or one started with the -i option. PS1 is set and $- includes i if bash is interactive,allowing a shell script or a startup file to test this state.
老高注解:

判断一个交互式shell也很简单echo $-的结果中包含小写字母i就是了。

当你运行一个脚本时,那就是一个非交互式shell。
When bash is started non-interactively, to run a shell script, for example, it looks for the variable BASH_ENV in the environment, expands its value if it appears there, and uses the expanded value as the name of a file to read and execute. Bash behaves as if the following command were executed:
if [ -n "$BASH_ENV" ]; then . "$BASH_ENV"; fi
but the value of the PATH variable is not used to search for the file name.
老高翻译:

当一个非交互的shell运行一个shell脚本时,他会在环境变量中 $BASH_ENV 并执行。代码为if [ -n "$BASH_ENV" ]; then . "$BASH_ENV"; fi。

The following paragraphs describe how bash executes its startup files. If any of the files exist but cannot be read, bash reports an error. Tildes are expanded in file names as described below under Tilde Expansion in the EXPANSION section.
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.
老高翻译:

当你登录一个交互式的shell后,如果/etc/profile存在,bash首先会读取/etc/profile的配置,然后再依次读取对应用户的~/.bash_profile、~/.bash_login和~/.profile。

When a login shell exits, bash reads and executes commands from the file ~/.bash_logout, if it exists.
老高翻译:

当一个登录shell退出的时候,如果当前用户的~/.bash_logout存在,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.
老高翻译:

当一个非登陆shell运行,如果对应用户的~/.bashrc存在,bash首先会读取~/.bashrc,有个--norc option能阻止读取,还有个--rcfile的选项可以强制读取指定文件。

综上:

~/.bashrc与/etc/bashrc、~/.bash_profile与/etc/profile的关系一样,都是一个针对某个用户,一个针对全局设置。读取的时候也是全局优先。

交互式shell登录时读取profile,初始化时读取bashrc。

非交互式只会读取bashrc。

一般把alias和function一类的放到bashrc或~/.bashrc中。

而把export更多的放在profile文件中。

推荐大家参考My bashrc, bash aliases, profile and other files的配置;

同时安装Linux下最好的shell :zsh + oh-my-zsh 默认shell的最佳替代品;

如果还是钟情与bash,那么老高推荐你安装bash-completion,自动补全的功能很好用!

本文来源:http://www.bbyears.com/caozuoxitong/79928.html

热门标签

更多>>

本类排行