bash .bashrc、.bash_profile 和 .environment 之间有什么区别?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/415403/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
What's the difference between .bashrc, .bash_profile, and .environment?
提问by Adam Rosenfield
I've used a number of different *nix-based systems of the years, and it seems like every flavor of Bash I use has a different algorithm for deciding which startup scripts to run. For the purposes of tasks like setting up environment variables and aliases and printing startup messages (e.g. MOTDs), which startup script is the appropriate place to do these?
多年来,我使用了许多不同的基于 *nix 的系统,似乎我使用的每种 Bash 风格都有不同的算法来决定运行哪些启动脚本。对于诸如设置环境变量和别名以及打印启动消息(例如 MOTD)之类的任务,哪个启动脚本是执行这些操作的合适位置?
What's the difference between putting things in .bashrc
, .bash_profile
, and .environment
? I've also seen other files such as .login
, .bash_login
, and .profile
; are these ever relevant? What are the differences in which ones get run when logging in physically, logging in remotely via ssh, and opening a new terminal window? Are there any significant differences across platforms (including Mac OS X (and its Terminal.app) and Cygwin Bash)?
是什么把事情之间的差异.bashrc
,.bash_profile
以及.environment
?我也看到其他的文件,例如.login
,.bash_login
和.profile
; 这些是否相关?在物理登录、通过 ssh 远程登录和打开新的终端窗口时运行的有什么区别?跨平台(包括 Mac OS X(及其 Terminal.app)和 Cygwin Bash)是否存在显着差异?
采纳答案by Cos
The main difference with shell config files is that some are only read by "login" shells (eg. when you login from another host, or login at the text console of a local unix machine). these are the ones called, say, .login
or .profile
or .zlogin
(depending on which shell you're using).
与 shell 配置文件的主要区别在于,有些文件只能由“登录”shell 读取(例如,当您从另一台主机登录时,或在本地 unix 机器的文本控制台上登录时)。这些是所谓的,比如说,.login
或.profile
或.zlogin
(取决于您使用的外壳)。
Then you have config files that are read by "interactive" shells (as in, ones connected to a terminal (or pseudo-terminal in the case of, say, a terminal emulator running under a windowing system). these are the ones with names like .bashrc
, .tcshrc
, .zshrc
, etc.
然后你有由“交互式”外壳读取的配置文件(例如,连接到终端(或伪终端,例如,在窗口系统下运行的终端模拟器)。这些是具有名称的像.bashrc
,.tcshrc
,.zshrc
等等。
bash
complicates this in that .bashrc
is only read by a shell that's both interactiveand non-login, so you'll find most people end up telling their .bash_profile
to also read .bashrc
with something like
bash
使这一点复杂化,因为.bashrc
它只能由交互式和非登录的 shell 读取,所以你会发现大多数人最终告诉他们.bash_profile
也.bashrc
用类似的东西阅读
[[ -r ~/.bashrc ]] && . ~/.bashrc
[[ -r ~/.bashrc ]] && . ~/.bashrc
Other shells behave differently - eg with zsh
, .zshrc
is always read for an interactive shell, whether it's a login one or not.
其他外壳的行为有所不同 - 例如zsh
,.zshrc
对于交互式外壳,无论是否登录,始终读取 , 。
The manual page for bash explains the circumstances under which each file is read. Yes, behaviour is generally consistent between machines.
bash 的手册页解释了读取每个文件的情况。是的,机器之间的行为通常是一致的。
.profile
is simply the login script filename originally used by /bin/sh
. bash
, being generally backwards-compatible with /bin/sh
, will read .profile
if one exists.
.profile
只是最初使用的登录脚本文件名/bin/sh
。bash
,通常与 向后兼容/bin/sh
,.profile
如果存在,将读取。
回答by Johannes Schaub - litb
That's simple. It's explained in man bash
:
这很简单。它在man bash
:
/bin/bash
The bash executable
/etc/profile
The systemwide initialization file, executed for login shells
~/.bash_profile
The personal initialization file, executed for login shells
~/.bashrc
The individual per-interactive-shell startup file
~/.bash_logout
The individual login shell cleanup file, executed when a login shell exits
~/.inputrc
Individual readline initialization file
Login shells are the ones that are read one you login (so, they are not executed when merely starting up xterm, for example). There are other ways to login. For example using an X display manager. Those have other ways to read and export environment variables at login time.
登录 shell 是读取您登录的 shell(因此,例如,仅在启动 xterm 时不会执行它们)。还有其他登录方式。例如使用 X 显示管理器。那些有其他方式在登录时读取和导出环境变量。
Also read the INVOCATION
chapter in the manual. It says "The following paragraphs describe how bash executes its startup files.", i think that's a spot-on :) It explains what an "interactive" shell is too.
另请阅读INVOCATION
手册中的章节。它说“以下段落描述了 bash 如何执行其启动文件。” ,我认为这是一个点:) 它也解释了“交互式”外壳是什么。
Bash does not know about .environment
. I suspect that's a file of your distribution, to set environment variables independent of the shell that you drive.
Bash 不知道.environment
. 我怀疑这是您的发行版文件,用于设置独立于您驱动的 shell 的环境变量。
回答by Jonathan Leffler
Classically, ~/.profile
is used by Bourne Shell, and is probably supported by Bash as a legacy measure. Again, ~/.login
and ~/.cshrc
were used by C Shell - I'm not sure that Bash uses them at all.
传统上,~/.profile
由 Bourne Shell 使用,并且可能被 Bash 支持作为遗留措施。同样,~/.login
和~/.cshrc
使用由C外壳-我不知道Bash使用它们。
The ~/.bash_profile
would be used once, at login. The ~/.bashrc
script is read every time a shell is started. This is analogous to /.cshrc
for C Shell.
在~/.bash_profile
将被使用一次,在登录。~/.bashrc
每次启动 shell 时都会读取该脚本。这类似于/.cshrc
C Shell。
One consequence is that stuff in ~/.bashrc
should be as lightweight (minimal) as possible to reduce the overhead when starting a non-login shell.
一个结果是输入的内容~/.bashrc
应该尽可能轻量(最小)以减少启动非登录 shell 时的开销。
I believe the ~/.environment
file is a compatibility file for Korn Shell.
我相信该~/.environment
文件是 Korn Shell 的兼容文件。
回答by Filip Ekberg
I found information about .bashrc and .bash_profile hereto sum it up:
我在这里找到了关于 .bashrc 和 .bash_profile 的信息来总结一下:
.bash_profile is executed when you login. Stuff you put in there might be your PATH and other important environment variables.
.bashrc is used for non login shells. I'm not sure what that means. I know that RedHat executes it everytime you start another shell (su to this user or simply calling bash again) You might want to put aliases in there but again I am not sure what that means. I simply ignore it myself.
.profile is the equivalent of .bash_profile for the root. I think the name is changed to let other shells (csh, sh, tcsh) use it as well. (you don't need one as a user)
There is also .bash_logout wich executes at, yeah good guess...logout. You might want to stop deamons or even make a little housekeeping . You can also add "clear" there if you want to clear the screen when you log out.
.bash_profile 在您登录时执行。您放入的内容可能是您的 PATH 和其他重要的环境变量。
.bashrc 用于非登录 shell。我不确定这意味着什么。我知道 RedHat 每次启动另一个 shell 时都会执行它(su 给这个用户或简单地再次调用 bash)你可能想在那里放别名,但我又不确定这意味着什么。我只是自己忽略它。
.profile 相当于 root 的 .bash_profile 。我认为更改名称是为了让其他 shell(csh、sh、tcsh)也可以使用它。(你不需要一个作为用户)
还有 .bash_logout 执行,是的,很好的猜测......注销。您可能想要停止 deamons 或什至做一点家务。如果您想在注销时清除屏幕,也可以在此处添加“清除”。
Also there is a complete follow up on each of the configurations files here
也有是在每个配置文件的完整随访这里
These are probably even distro.-dependant, not all distros choose to have each configuraton with them and some have even more. But when they have the same name, they usualy include the same content.
这些甚至可能依赖于发行版,并非所有发行版都选择使用每个配置,有些发行版甚至更多。但是当它们具有相同的名称时,它们通常包含相同的内容。
回答by Rose Perrone
According to Josh Staiger, Mac OS X's Terminal.app actually runs a login shell rather than a non-login shell by default for each new terminal window, calling .bash_profile instead of .bashrc.
根据Josh Staiger 的说法,Mac OS X 的 Terminal.app 实际上为每个新的终端窗口默认运行登录 shell 而不是非登录 shell,调用 .bash_profile 而不是 .bashrc。
He recommends:
他建议:
Most of the time you don't want to maintain two separate config files for login and non-login shells — when you set a PATH, you want it to apply to both. You can fix this by sourcing .bashrc from your .bash_profile file, then putting PATH and common settings in .bashrc.
To do this, add the following lines to .bash_profile:
if [ -f ~/.bashrc ]; then source ~/.bashrc fi
Now when you login to your machine from a console .bashrc will be called.
大多数情况下,您不想为登录和非登录 shell 维护两个单独的配置文件——当您设置 PATH 时,您希望它适用于两者。您可以通过从 .bash_profile 文件中获取 .bashrc 来解决此问题,然后将 PATH 和常用设置放在 .bashrc 中。
为此,请将以下几行添加到 .bash_profile:
if [ -f ~/.bashrc ]; then source ~/.bashrc fi
现在,当您从控制台登录到您的机器时,将调用 .bashrc。
回答by PolyThinker
回答by seismick
I have used Debian-family distros which appear to execute .profile
, but not .bash_profile
,
whereas RHEL derivatives execute .bash_profile
before .profile
.
我使用过 Debian 系列发行版,它们似乎可以执行.profile
,但不能执行.bash_profile
,而 RHEL 衍生品则.bash_profile
在.profile
.
It seems to be a mess when you have to set up environment variables to work in any Linux OS.
当您必须设置环境变量以在任何 Linux 操作系统中工作时,这似乎是一团糟。