.bashrc、.bash_profile 和 .environment 之间有什么区别?

发布于 2024-07-10 11:49:28 字数 414 浏览 10 评论 0原文

多年来我使用过许多不同的基于 *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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

笔芯 2024-07-17 11:49:28

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

情愿 2024-07-17 11:49:28

这很简单。 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

登录 shell 是您登录时读取的 shell(因此,例如,仅在启动 xterm 时它们不会执行)。 还有其他登录方式。 例如使用 X 显示管理器。 这些还有其他方法可以在登录时读取和导出环境变量。

另请阅读手册中的INVOCATION章节。 它说“以下段落描述了 bash 如何执行其启动文件。”,我认为这是正确的:)它也解释了“交互式”shell 是什么。

Bash 不知道 .environment。 我怀疑这是您的发行版的一个文件,用于设置独立于您驱动的 shell 的环境变量。

That's simple. It's explained in 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.

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.

执笏见 2024-07-17 11:49:28

传统上,~/.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.

一枫情书 2024-07-17 11:49:28

我在此处找到了有关 .bashrc 和 .bash_profile 的信息来总结它向上:

.bash_profile 在您执行时执行
登录。 你放在那里的东西可能是
你的路径和其他重要的
环境变量。

.bashrc 用于非登录 shell。
我不确定这是什么意思。 我知道
那个红帽
每次启动时执行
另一个 shell(su 到该用户或
只需再次调用 bash)你可能会
想要在那里添加别名,但再次
我不确定这意味着什么。 我
我自己直接忽略它。

.profile 相当于
.bash_profile 为根目录。 我认为
更改名称是为了让其他人
shell(csh、sh、tcsh)也使用它。
(您不需要作为用户)

还有 .bash_logout wich
执行于,是的,不错的猜测...注销。
你可能想阻止恶魔,甚至
做点家务。 你可以
如果您愿意,还可以在那里添加“清除”
注销时清除屏幕。

此外,每个配置文件都有完整的后续操作 这里

这些甚至可能与发行版相关,并非所有发行版都选择使用它们的每个配置,有些发行版甚至有更多。 但当它们具有相同的名称时,它们通常包含相同的内容。

I found information about .bashrc and .bash_profile here to sum it up:

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

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.

无法回应 2024-07-17 11:49:28

根据 Josh Staiger 的说法,Mac OS X 的 Terminal.app 实际上运行登录默认情况下,每个新终端窗口都会使用 shell 而不是非登录 shell,调用 .bash_profile 而不是 .bashrc。

他建议:

大多数时候您不想维护两个单独的配置文件
对于登录和非登录 shell — 当您设置 PATH 时,您希望它
适用于两者。 您可以通过从您的 .bashrc 中获取来解决此问题
.bash_profile 文件,然后将 PATH 和常用设置放入 .bashrc 中。

为此,请将以下行添加到 .bash_profile:

if [ -f ~/.bashrc ];   然后  
      源~/.bashrc  
  菲 
  

现在,当您登录到您的
将从控制台 .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:

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.

我一直都在从未离去 2024-07-17 11:49:28

bash 的手册页是一个值得查看的好地方。 这里是在线版本。 查找“调用”部分。

A good place to look at is the man page of bash. Here's an online version. Look for "INVOCATION" section.

嗳卜坏 2024-07-17 11:49:28

我使用过 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文