bash 为什么我必须继续使用 `source ~/.profile` 来获取设置?

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

Why do I have to keep using `source ~/.profile` to get settings in place?

macosbashterminal

提问by locoboy

I have a couple of bash scripts that I want to make sure runs by default and I'm currently storing them in ~/.profileon my mac. Is that the wrong place to be storing them? I've heard of others and tried them (like ~/.bashrc, ~/.bash_profile, etc), but they don't seem to be working.

我有几个 bash 脚本,我想确保它们在默认情况下运行,我目前将它们存储在~/.profile我的 mac 上。那是存放它们的错误地方吗?我听说过别人,并试图他们(如~/.bashrc~/.bash_profile等),但他们似乎并不奏效。

What is the difference between all of these and which one do I put the scripts in so that it configures on runtime and I don't have to call $ source ~/.profileevery time I open the terminal?

所有这些之间有什么区别,我将脚本放在哪一个中以便它在运行时进行配置并且$ source ~/.profile每次打开终端时都不必调用?

采纳答案by kev

                     +-----------------+
                     |                 |
interactive shell -->|  ~/.bashrc      |
                     |                 |
                     +-----------------+

interactive shellwill source ~/.bashrcautomatically.

interactive shell~/.bashrc自动来源。

Take a look at Should the .bashrc in the home directory load automatically?

看看主目录中的 .bashrc 是否应该自动加载?

回答by Lri

If both ~/.bash_profileand ~/.profileexist, bash only reads ~/.bash_profilewhen it is invoked as an interactive login shell.

如果同时~/.bash_profile~/.profile存在时,bash只读取~/.bash_profile,当它被作为交互登录shell。

https://www.gnu.org/s/bash/manual/html_node/Bash-Startup-Files.html:

https://www.gnu.org/s/bash/manual/html_node/Bash-Startup-Files.html

Invoked as an interactive login shell, or with --login

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.

[...]

Invoked as an interactive non-login shell

When an interactive shell that is not a login shell is started, Bash reads and executes commands from ~/.bashrc, if that file exists.

作为交互式登录 shell 调用,或使用 --login

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

[...]

作为交互式非登录 shell 调用

当一个不是登录 shell 的交互式 shell 启动时,Bash 读取并执行来自 的命令~/.bashrc(如果该文件存在)。

~/.profileis also used by other shells.

~/.profile其他shell也使用。

Terminal and iTerm open new shells as login shells by default (by executing something like login -pf $USER), but many GNU/Linux terminal applications open new shells as non-login shells. OS X users often use ~/.bash_profileinstead of ~/.bashrc.

默认情况下,终端和 iTerm 将新外壳打开为登录外壳(通过执行类似的操作login -pf $USER),但许多 GNU/Linux 终端应用程序将新外壳打开为非登录外壳。OS X用户经常使用~/.bash_profile的替代~/.bashrc

回答by user1970485

I did these to rectify the problem:

我做了这些来纠正问题:

cat .bash_profile >> .profile
rm .bash_profile

the alternative is:

替代方案是:

echo "source ~/.profile" >> .bash_profile

回答by D Hymanson

Make sure if you do source ~/.profilein your .bashrcthat you comment out or remove any commands (in .profile) to call or source .bashrcin your .profileor it will loop forever and you will never get a prompt.

确保如果你做source ~/.profile你的.bashrc,你注释掉或删除任何命令(在.profile打电话或源).bashrc在你的.profile,否则将永远循环下去,你将永远不会得到提示。

回答by David Souther

Different setups of bash will automatically source different files depending on their configuration. The nearly universal file that is always sourced is ~/.bashrc- this is a bash core thing that it will load this file. In that file, you should add your line to source ~/.profileand you'll be good to go!

bash 的不同设置将根据其配置自动获取不同的文件。始终提供的几乎通用的文件是~/.bashrc- 这是将加载此文件的 bash 核心内容。在那个文件中,你应该添加你的行,source ~/.profile然后你就可以开始了!

-Edit-

-编辑-

From my Linux and my colleague's Mac:

在我的 Linux 和我同事的 Mac 上:

$ echo "echo hello" >> ~/.profile
$ echo "source ~/.profile" >> ~/.bashrc
$ bash
Hello
$