关于.bash_profile,.bashrc,以及别名应写在哪里? [重复]

时间:2022-09-12 23:24:57

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 bash again and that alias will be in effect.

在Mac OS X上的.bashrc中,新创建的shell将没有该别名。我需要再次键入bash,该别名将生效。

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

如果我在托管公司登录Linux,.bashrc文件有一条注释行,上面写着:

For non-login shell

对于非登录shell

and the .bash_profile file has a comment that says

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

for login shell

用于登录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.

有些网页说使用.bash_aliases,但它似乎不适用于Mac OS X.

4 个解决方案

#1


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

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

Personally,

  • I put my PATH setup into a .profile file (because I sometimes use other shells);
  • 我将PATH设置放入.profile文件中(因为我有时使用其他shell);

  • I put my Bash aliases and functions into my .bashrc file;
  • 我将我的Bash别名和函数放入我的.bashrc文件中;

  • 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_profile file.

    在我的.bash_profile文件中。

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

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

source ~/.bashrc

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

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

#2


Check out http://mywiki.wooledge.org/DotFiles for 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_profile or ~/.profile is read and executed. Since everything you run from your login shell inherits the login shell's environment, you should put all your environment variables in there. Like LESS, PATH, MANPATH, LC_*, ... For an example, see: My .profile
  • 您只需登录一次,这就是读取和执行〜/ .bash_profile或〜/ .profile的时间。由于您从登录shell运行的所有内容都继承了登录shell的环境,因此您应该将所有环境变量放在那里。像LESS,PATH,MANPATH,LC_ *,......例如,请参阅:我的.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 /.profile or ~/.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 set and shopt, etc. For an example, see: My .bashrc
  • 登录后,您可以运行多个shell。想象一下登录,运行X,并在X中使用bash shell启动一些终端。这意味着您的登录shell启动了X,它继承了您的登录shell的环境变量,它启动了您的终端,启动了您的非登录bash shell。您的环境变量在整个链中传递,因此您的非登录shell不再需要加载它们。由于这个原因,非登录shell只执行〜/ .bashrc,而不是/.profile或〜/ .bash_profile,因此在那里定义仅适用于bash的所有内容。这是函数,别名,仅限bash的变量,如HISTSIZE(这不是环境变量,不导出它!),带有set和shopt的shell选项等。例如,参见:My .bashrc

  • Now, as part of UNIX peculiarity, a login-shell does NOT execute ~/.bashrc but only ~/.profile or ~/.bash_profile, so you should source that one manually from the latter. You'll see me do that in my ~/.profile too: source ~/.bashrc.
  • 现在,作为UNIX特性的一部分,login-shell不执行〜/ .bashrc而只执行〜/ .profile或〜/ .bash_profile,因此您应该从后者手动获取一个。你会看到我在〜/ .profile中也这样做:source~ / .bashrc。

#3


From the bash manpage:

从bash手册页:

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

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

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

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

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

当启动不是登录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 .bashrc or .bash_profile, and then have the other file source the first one.

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

#4


.bash_profile is 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被加载为“登录shell”。我不确定在OS X上会是什么,但在X11或虚拟终端的Linux上。

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

每次运行Bash时都会加载.bashrc。这是你应该在每次打开一个新的Terminal.app窗口时放入你想要加载的东西的地方。

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

我个人把所有内容放在.bashrc中,这样我就不必重新启动应用程序以使更改生效。

#1


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

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

Personally,

  • I put my PATH setup into a .profile file (because I sometimes use other shells);
  • 我将PATH设置放入.profile文件中(因为我有时使用其他shell);

  • I put my Bash aliases and functions into my .bashrc file;
  • 我将我的Bash别名和函数放入我的.bashrc文件中;

  • 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_profile file.

    在我的.bash_profile文件中。

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

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

source ~/.bashrc

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

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

#2


Check out http://mywiki.wooledge.org/DotFiles for 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_profile or ~/.profile is read and executed. Since everything you run from your login shell inherits the login shell's environment, you should put all your environment variables in there. Like LESS, PATH, MANPATH, LC_*, ... For an example, see: My .profile
  • 您只需登录一次,这就是读取和执行〜/ .bash_profile或〜/ .profile的时间。由于您从登录shell运行的所有内容都继承了登录shell的环境,因此您应该将所有环境变量放在那里。像LESS,PATH,MANPATH,LC_ *,......例如,请参阅:我的.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 /.profile or ~/.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 set and shopt, etc. For an example, see: My .bashrc
  • 登录后,您可以运行多个shell。想象一下登录,运行X,并在X中使用bash shell启动一些终端。这意味着您的登录shell启动了X,它继承了您的登录shell的环境变量,它启动了您的终端,启动了您的非登录bash shell。您的环境变量在整个链中传递,因此您的非登录shell不再需要加载它们。由于这个原因,非登录shell只执行〜/ .bashrc,而不是/.profile或〜/ .bash_profile,因此在那里定义仅适用于bash的所有内容。这是函数,别名,仅限bash的变量,如HISTSIZE(这不是环境变量,不导出它!),带有set和shopt的shell选项等。例如,参见:My .bashrc

  • Now, as part of UNIX peculiarity, a login-shell does NOT execute ~/.bashrc but only ~/.profile or ~/.bash_profile, so you should source that one manually from the latter. You'll see me do that in my ~/.profile too: source ~/.bashrc.
  • 现在,作为UNIX特性的一部分,login-shell不执行〜/ .bashrc而只执行〜/ .profile或〜/ .bash_profile,因此您应该从后者手动获取一个。你会看到我在〜/ .profile中也这样做:source~ / .bashrc。

#3


From the bash manpage:

从bash手册页:

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

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

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

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

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

当启动不是登录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 .bashrc or .bash_profile, and then have the other file source the first one.

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

#4


.bash_profile is 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被加载为“登录shell”。我不确定在OS X上会是什么,但在X11或虚拟终端的Linux上。

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

每次运行Bash时都会加载.bashrc。这是你应该在每次打开一个新的Terminal.app窗口时放入你想要加载的东西的地方。

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

我个人把所有内容放在.bashrc中,这样我就不必重新启动应用程序以使更改生效。