.bashrc、.bash_profile 和 .environment 之间有什么区别?
多年来我使用过许多不同的基于 *nix 的系统,似乎我使用的每种 Bash 风格都有不同的算法来决定运行哪些启动脚本。 对于设置环境变量和别名以及打印启动消息(例如 MOTD)等任务,哪个启动脚本是执行这些任务的合适位置?
将内容放入 .bashrc
、.bash_profile
和 .environment
之间有什么区别? 我还看到了其他文件,例如 .login
、.bash_login
和 .profile
; 这些是否相关? 物理登录、通过 ssh 远程登录以及打开新终端窗口时运行的程序有什么区别? 跨平台(包括 Mac OS X(及其 Terminal.app)和 Cygwin Bash)是否存在显着差异?
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?
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)?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
与 shell 配置文件的主要区别在于,有些配置文件只能由“登录”shell 读取(例如,当您从另一台主机登录或在本地 UNIX 计算机的文本控制台登录时)。 这些是名为
.login
或.profile
或.zlogin
的内容(取决于您使用的 shell)。然后你就有了由“交互式”shell 读取的配置文件(例如,连接到终端的配置文件(或伪终端,例如在窗口系统下运行的终端模拟器)。这些是具有名称的文件例如
.bashrc
、.tcshrc
、.zshrc
等。bash
使.bashrc 的情况变得复杂
只能由交互式和非登录的 shell 读取,因此您会发现大多数人最终都会告诉他们.bash_profile
还可以使用[[ -r ~/.bashrc ]] & ~/.bashrc
读取.bashrc
。其他 shell 的行为有所不同 - 例如,使用
zsh
时,总是会读取交互式 shell,无论它是否是登录 shell。bash的手册页解释了以下情况。是的,机器之间的行为通常是一致的。
.profile
只是/bin/sh
最初使用的登录脚本文件名。bash
通常与/bin/sh
向后兼容,将读取.profile
(如果存在)。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).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.bash
complicates this in that.bashrc
is only read by a shell that's both interactive and non-login, so you'll find most people end up telling their.bash_profile
to also read.bashrc
with something like[[ -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.The manual page for bash explains the circumstances under which each file is read. Yes, behaviour is generally consistent between machines.
.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.这很简单。 man bash 中对此进行了解释:
登录 shell 是您登录时读取的 shell(因此,例如,仅在启动 xterm 时它们不会执行)。 还有其他登录方式。 例如使用 X 显示管理器。 这些还有其他方法可以在登录时读取和导出环境变量。
另请阅读手册中的
INVOCATION
章节。 它说“以下段落描述了 bash 如何执行其启动文件。”,我认为这是正确的:)它也解释了“交互式”shell 是什么。Bash 不知道
.environment
。 我怀疑这是您的发行版的一个文件,用于设置独立于您驱动的 shell 的环境变量。That's simple. It's explained in
man bash
: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.
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.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.传统上,
~/.profile
由 Bourne Shell 使用,并且可能作为遗留措施受到 Bash 支持。 同样,~/.login
和~/.cshrc
由 C Shell 使用 - 我不确定 Bash 是否使用它们。~/.bash_profile
将在登录时使用一次。 每次启动 shell 时都会读取~/.bashrc
脚本。 这类似于 C Shell 的/.cshrc
。一个结果是
~/.bashrc
中的内容应该尽可能轻量(最小),以减少启动非登录 shell 时的开销。我相信
~/.environment
文件是 Korn Shell 的兼容性文件。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.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.One consequence is that stuff in
~/.bashrc
should be as lightweight (minimal) as possible to reduce the overhead when starting a non-login shell.I believe the
~/.environment
file is a compatibility file for Korn Shell.我在此处找到了有关 .bashrc 和 .bash_profile 的信息来总结它向上:
此外,每个配置文件都有完整的后续操作 这里
这些甚至可能与发行版相关,并非所有发行版都选择使用它们的每个配置,有些发行版甚至有更多。 但当它们具有相同的名称时,它们通常包含相同的内容。
I found information about .bashrc and .bash_profile here to sum it up:
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.
根据 Josh Staiger 的说法,Mac OS X 的 Terminal.app 实际上运行登录默认情况下,每个新终端窗口都会使用 shell 而不是非登录 shell,调用 .bash_profile 而不是 .bashrc。
他建议:
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.
He recommends:
bash 的手册页是一个值得查看的好地方。 这里是在线版本。 查找“调用”部分。
A good place to look at is the man page of bash. Here's an online version. Look for "INVOCATION" section.
我使用过 Debian 系列发行版,它们似乎执行
.profile
,但不执行.bash_profile
,而 RHEL 衍生版本在
.profile
之前执行.bash_profile
。当你必须设置环境变量才能在任何 Linux 操作系统中工作时,这似乎很混乱。
I have used Debian-family distros which appear to execute
.profile
, but not.bash_profile
,whereas RHEL derivatives execute
.bash_profile
before.profile
.It seems to be a mess when you have to set up environment variables to work in any Linux OS.