bash 关于.bash_profile、.bashrc,别名应该写在哪里?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/902946/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 18:12:44  来源:igfitidea点击:

About .bash_profile, .bashrc, and where should alias be written in?

bashshellenvironment

提问by nonopolarity

Possible Duplicate:What's the difference between .bashrc, .bash_profile, and .environment?

可能的重复:.bashrc、.bash_profile 和 .environment 之间有什么区别?

It seems that if I use

看来,如果我使用

alias ls='ls -F'

inside of .bashrc on Mac OS X, then the newly created shell will not have that alias. I need to type bashagain and that alias will be in effect.

在 Mac OS X 上的 .bashrc 内部,那么新创建的 shell 将没有该别名。我需要bash再次输入,该别名才会生效。

And if I log into Linux on the hosting company, the .bashrcfile has a comment line that says:

如果我在托管公司上登录 Linux,该.bashrc文件有一条注释行,内容为:

For non-login shell

对于非登录shell

and the .bash_profilefile has a comment that says

并且该.bash_profile文件有一条评论说

for login shell

用于登录外壳

So where should aliases be written in? How come we separate the login shell and non-login shell?

那么别名应该写在哪里呢?我们如何将登录shell和非登录shell分开?

Some webpage say use .bash_aliases, but it doesn't work on Mac OS X, it seems.

有些网页说 use .bash_aliases,但它似乎在 Mac OS X 上不起作用。

回答by Charlie Martin

The reason you separate the loginand non-loginshell is because the .bashrcfile is reloaded every time you start a new copy of Bash. The .profilefile is loaded only when you either log in or use the appropriate flag to tell Bash to act as a login shell.

登录非登录shell分开的原因是因为.bashrc每次启动 Bash 的新副本时都会重新加载该文件。.profile仅当您登录或使用适当的标志告诉 Bash 作为登录 shell 时才会加载该文件。

Personally,

亲身,

  • I put my PATHsetup into a .profilefile (because I sometimes use other shells);
  • I put my Bash aliases and functions into my .bashrcfile;
  • I put this

    #!/bin/bash
    #
    # CRM .bash_profile Time-stamp: "2008-12-07 19:42"
    #
    # echo "Loading ${HOME}/.bash_profile"
    source ~/.profile # get my PATH setup
    source ~/.bashrc  # get my Bash aliases
    

    in my .bash_profilefile.

  • 我将我的PATH设置放入一个.profile文件中(因为我有时会使用其他 shell);
  • 我将我的 Bash 别名和函数放入我的.bashrc文件中;
  • 我把这个

    #!/bin/bash
    #
    # CRM .bash_profile Time-stamp: "2008-12-07 19:42"
    #
    # echo "Loading ${HOME}/.bash_profile"
    source ~/.profile # get my PATH setup
    source ~/.bashrc  # get my Bash aliases
    

    在我的.bash_profile文件中。

Oh, and the reason you need to type bashagain to get the new alias is that Bash loads your .bashrcfile when it starts but it doesn't reload it unless you tell it to. You can reload the .bashrcfile (and not need a second shell) by typing

哦,您需要bash再次键入以获取新别名的原因是 Bash.bashrc在启动时加载您的文件,但除非您告诉它,否则它不会重新加载它。您可以.bashrc通过键入重新加载文件(并且不需要第二个外壳)

source ~/.bashrc

which loads the .bashrcfile as if you had typed the commands directly to Bash.

它加载.bashrc文件,就像您直接在 Bash 中键入命令一样。

回答by lhunath

Check out http://mywiki.wooledge.org/DotFilesfor an excellent resource on the topic aside from man bash.

查看 http://mywiki.wooledge.org/DotFiles除了man bash.

Summary:

概括:

  • You only log in once, and that's when ~/.bash_profileor ~/.profileis read and executed. Since everything you run from your login shell inherits the login shell's environment, you should put all your environment variablesin there. Like LESS, PATH, MANPATH, LC_*, ... For an example, see: My .profile
  • Once you log in, you can run several more shells. Imagine logging in, running X, and in X starting a few terminals with bash shells. That means your login shell started X, which inherited your login shell's environment variables, which started your terminals, which started your non-login bash shells. Your environment variables were passed along in the whole chain, so your non-login shells don't need to load them anymore. Non-login shells only execute ~/.bashrc, not /.profileor ~/.bash_profile, for this exact reason, so in there define everything that only applies to bash. That's functions, aliases, bash-only variables like HISTSIZE (this is not an environment variable, don't export it!), shell options with setand shopt, etc. For an example, see: My .bashrc
  • Now, as part of UNIX peculiarity, a login-shell does NOT execute ~/.bashrcbut only ~/.profileor ~/.bash_profile, so you should source that one manually from the latter. You'll see me do that in my ~/.profiletoo: source ~/.bashrc.
  • 您只需登录一次,就可以读取或执行~/.bash_profileor ~/.profile。由于您从登录 shell 运行的所有内容都继承了登录 shell 的环境,因此您应该将所有环境变量放在那里。像LESS, PATH, MANPATH, LC_*, ... 例如,请参阅:我的.profile
  • 登录后,您可以运行多个 shell。想象一下登录,运行 X,然后在 X 中使用 bash shell 启动几个终端。这意味着你的登录 shell 启动了 X,它继承了你的登录 shell 的环境变量,它启动了你的终端,它启动了你的非登录 bash shell。您的环境变量在整个链中传递,因此您的非登录 shell 不再需要加载它们。出于这个确切原因,非登录 shell 只执行~/.bashrc,不执行/.profileor ~/.bash_profile,因此在那里定义仅适用于 bash 的所有内容。那是函数、别名、仅 bash 的变量,如 HISTSIZE(这不是环境变量,不要导出它!)、带有setand 的shell 选项shopt等。例如,请参见:我的 .bashrc
  • 现在,作为 UNIX 特性的一部分,登录 shell 不执行~/.bashrc而只执行~/.profileor ~/.bash_profile,因此您应该从后者手动获取那个。你会看到我也在我的~/.profile: 中这样做source ~/.bashrc

回答by Adam Rosenfield

From the bash manpage:

从 bash 联机帮助页:

When bash is invoked as an interactive login shell, or as a non-interactive shell with the --loginoption, 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 --noprofileoption may be used when the shell is started to inhibit this behavior.

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

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 --norcoption. The --rcfilefile option will force bash to read and execute commands from file instead of ~/.bashrc.

当 bash 作为交互式登录 shell 或作为具有--login选项的非交互式 shell 调用时 ,它首先从文件中读取并执行命令 /etc/profile(如果该文件存在)。读取该文件后,它会按该顺序查找 ~/.bash_profile~/.bash_login、 和 ~/.profile,并从第一个存在且可读的命令中读取和执行命令。--noprofile当 shell 启动时可以使用该 选项来禁止这种行为。

当登录 shell 退出时,bash 从文件中读取并执行命令~/.bash_logout(如果存在)。

当一个不是登录 shell 的交互式 shell 启动时,bash 读取并执行来自 的命令~/.bashrc(如果该文件存在)。这可以通过使用--norc选项来禁止。该--rcfile文件选项将强制bash读取和文件,而不是执行命令 ~/.bashrc

Thus, if you want to get the same behavior for both login shells and interactive non-login shells, you should put all of your commands in either .bashrcor .bash_profile, and then have the other file sourcethe first one.

因此,如果您想为登录 shell 和交互式非登录 shell 获得相同的行为,您应该将所有命令放在.bashrc或 中.bash_profile,然后将另一个文件放在第一个中。

回答by Sionide21

.bash_profileis loaded for a "login shell". I am not sure what that would be on OS X, but on Linux that is either X11 or a virtual terminal.

.bash_profile为“登录外壳”加载。我不确定在 OS X 上会是什么,但在 X11 或虚拟终端的 Linux 上。

.bashrcis loaded every time you run Bash. That is where you should put stuff you want loaded whenever you open a new Terminal.app window.

.bashrc每次运行 Bash 时都会加载。每当你打开一个新的 Terminal.app 窗口时,你应该把你想要加载的东西放在那里。

I personally put everything in .bashrcso that I don't have to restart the application for changes to take effect.

我个人把所有东西都放进去了,.bashrc这样我就不必重新启动应用程序以使更改生效。