.bashrc、.bash_profile和.environment的区别是什么?

时间:2022-05-27 23:25:17

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的味道都有一个不同的算法来决定运行哪个启动脚本。对于诸如设置环境变量和别名和打印启动消息(例如MOTDs)之类的任务,启动脚本是合适的地方吗?

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;这些是相关的吗?在物理日志记录、远程登录和打开新的终端窗口时,有哪些不同之处呢?平台之间是否存在显著差异(包括Mac OS X(以及它的终端应用程序)和Cygwin Bash)?

7 个解决方案

#1


73  

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(取决于您使用的shell)。

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.

然后,您就会有“交互式”shell读取的配置文件(例如,连接到终端的那些文件(比如,在窗口系统下运行的终端仿真程序)。这些名字有。bashrc,。tcshrc,。zshrc,等等。

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

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.

其他shell的行为不同——如zsh, .zshrc总是为交互式shell读取,不管它是否是登录的。

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。

#2


48  

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是读一个您登录的程序(例如,在仅仅启动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.

还可以阅读手册中的调用章节。它说“下面的段落描述了bash如何执行它的启动文件。”我认为这是一个亮点:它解释了什么是“交互式”shell。

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无关的环境变量。

#3


9  

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.

经典~ /。概要文件由Bourne Shell使用,并可能作为遗留度量来支持Bash。~ /。登录和~ /。cshrc被C Shell使用——我不确定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将在登录时使用一次。~ /。每当启动shell时,都会读取bashrc脚本。这类似于/。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.

其中一个后果就是~/。在启动非登录shell时,bashrc应该尽可能轻(最小)以减少开销。

I believe the ~/.environment file is a compatibility file for Korn Shell.

我相信~ /。环境文件是Korn Shell的兼容性文件。

#4


7  

I found information about .bashrc and .bash_profile here to 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.

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

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

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

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

.profile相当于root的.bash_profile。我认为这个名称被更改为让其他shell (csh, sh, tcsh)也使用它。(你不需要一个用户)

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_logout,在这里执行,是的,很好的猜测…退出。你可能会想要阻止deamons甚至做一些家务。如果你想在登出时清除屏幕,你也可以添加“clear”。

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.

这些甚至可能是发行版。-依赖者,不是所有的distros都选择每个配置,而有些则有更多。但是当他们有相同的名字时,他们通常会包含相同的内容。

#5


4  

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.

据乔希·斯泰格说,Mac OS X的终端。应用程序实际上是为每个新的终端窗口运行一个登录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.

大多数情况下,您不想为登录和非登录shell维护两个单独的配置文件——当您设置一个路径时,您希望它同时应用于这两个配置文件。您可以通过从.bash_profile文件中获取.bashrc来解决这个问题,然后在.bashrc中设置路径和通用设置。

To do this, add the following lines to .bash_profile:

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

if [ -f ~/.bashrc ]; then 
    source ~/.bashrc 
fi

Now when you login to your machine from a console .bashrc will be called.

现在,当您从控制台登录到您的机器时,将调用bashrc。

#6


2  

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

一个值得关注的好地方是bash的手册页。这是一个在线版本。寻找“调用”一节。

#7


0  

I have used Debian-family distros which appear to execute .profile, but not .bash_profile, whereas RHEL derivatives execute .bash_profile before .profile.

我已经使用了Debian-family distros,它看起来是执行.profile,但不是.bash_profile,而RHEL衍生工具执行.bash_profile。

It seems to be a mess when you have to set up environment variables to work in any Linux OS.

当您必须在任何Linux操作系统中设置环境变量时,这似乎是一种混乱。

#1


73  

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(取决于您使用的shell)。

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.

然后,您就会有“交互式”shell读取的配置文件(例如,连接到终端的那些文件(比如,在窗口系统下运行的终端仿真程序)。这些名字有。bashrc,。tcshrc,。zshrc,等等。

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

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.

其他shell的行为不同——如zsh, .zshrc总是为交互式shell读取,不管它是否是登录的。

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。

#2


48  

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是读一个您登录的程序(例如,在仅仅启动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.

还可以阅读手册中的调用章节。它说“下面的段落描述了bash如何执行它的启动文件。”我认为这是一个亮点:它解释了什么是“交互式”shell。

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无关的环境变量。

#3


9  

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.

经典~ /。概要文件由Bourne Shell使用,并可能作为遗留度量来支持Bash。~ /。登录和~ /。cshrc被C Shell使用——我不确定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将在登录时使用一次。~ /。每当启动shell时,都会读取bashrc脚本。这类似于/。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.

其中一个后果就是~/。在启动非登录shell时,bashrc应该尽可能轻(最小)以减少开销。

I believe the ~/.environment file is a compatibility file for Korn Shell.

我相信~ /。环境文件是Korn Shell的兼容性文件。

#4


7  

I found information about .bashrc and .bash_profile here to 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.

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

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

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

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

.profile相当于root的.bash_profile。我认为这个名称被更改为让其他shell (csh, sh, tcsh)也使用它。(你不需要一个用户)

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_logout,在这里执行,是的,很好的猜测…退出。你可能会想要阻止deamons甚至做一些家务。如果你想在登出时清除屏幕,你也可以添加“clear”。

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.

这些甚至可能是发行版。-依赖者,不是所有的distros都选择每个配置,而有些则有更多。但是当他们有相同的名字时,他们通常会包含相同的内容。

#5


4  

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.

据乔希·斯泰格说,Mac OS X的终端。应用程序实际上是为每个新的终端窗口运行一个登录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.

大多数情况下,您不想为登录和非登录shell维护两个单独的配置文件——当您设置一个路径时,您希望它同时应用于这两个配置文件。您可以通过从.bash_profile文件中获取.bashrc来解决这个问题,然后在.bashrc中设置路径和通用设置。

To do this, add the following lines to .bash_profile:

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

if [ -f ~/.bashrc ]; then 
    source ~/.bashrc 
fi

Now when you login to your machine from a console .bashrc will be called.

现在,当您从控制台登录到您的机器时,将调用bashrc。

#6


2  

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

一个值得关注的好地方是bash的手册页。这是一个在线版本。寻找“调用”一节。

#7


0  

I have used Debian-family distros which appear to execute .profile, but not .bash_profile, whereas RHEL derivatives execute .bash_profile before .profile.

我已经使用了Debian-family distros,它看起来是执行.profile,但不是.bash_profile,而RHEL衍生工具执行.bash_profile。

It seems to be a mess when you have to set up environment variables to work in any Linux OS.

当您必须在任何Linux操作系统中设置环境变量时,这似乎是一种混乱。