bash 如何让 OS X 读取 .bash_profile 而不是 .profile 文件

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

How to make OS X to read .bash_profile not .profile file

macosbash

提问by cherryhitech

I have read so many suggestions about, not putting your customization aka commands in ".profile" file. Rather, create a .bash_profile for yourself and add your alias and etc.

我已经阅读了很多关于不将自定义又名命令放在“.profile”文件中的建议。相反,为自己创建一个 .bash_profile 并添加您的别名等。

But,when I open the new terminal, if there is only .bash_profile, OS X is not exporting/sourcing the commands mentioned in it. I have to manually source the .bash_profile.

但是,当我打开新终端时,如果只有 .bash_profile,OS X 不会导出/采购其中提到的命令。我必须手动获取 .bash_profile。

If I create .profile file, on opening a new terminal, all my commands in .profile are executed and will be available readily.

如果我创建 .profile 文件,在打开一个新终端时,我在 .profile 中的所有命令都会被执行并且可以随时使用。

Could you please help me in understanding, how does it works? Also, when to use .bashrc/.profile/.bash_profile files.

你能帮我理解一下,它是如何工作的?此外,何时使用 .bashrc/.profile/.bash_profile 文件。

Thanks!

谢谢!

回答by Andon M. Coleman

According to the manual page that ships with OS X:

根据 OS X 附带的手册页:

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

...它按该顺序查找~/.bash_profile, ~/.bash_login, 和~/.profile, 并从第一个存在且可读的命令中读取和执行命令。--noprofile当 shell 启动时可以使用该选项来禁止这种行为。

It should only read ~/.profileas a last resort if neither ~/.bash_profilenor ~/.bash_loginare readable.

~/.profile如果既不可读~/.bash_profile也不~/.bash_login可读,它只能作为最后的手段阅读。

On all of my OS X systems, I have my ~/.bash_profileset to:

在我所有的 OS X 系统上,我都~/.bash_profile设置为:

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

It is highly recommended that you do this on OS X in order to get bash to read your ~/.bashrcfile like you would expect.

强烈建议您在 OS X 上执行此操作,以便让 bash~/.bashrc像您期望的那样读取您的文件。

回答by Matt S

It's also possible that your terminal shell is defaulting to sh instead of bash. You can verify this first:

也有可能您的终端外壳默认为 sh 而不是 bash。您可以先验证一下:

$ echo $SHELL
/bin/tcsh

To change this to bash, you can go into your Terminal -> Preferences -> Startup tab, and change "Shell Opens With:" from "Default login shell" to Command and value "/bin/bash".

要将其更改为 bash,您可以进入终端 -> 首选项 -> 启动选项卡,并将“Shell Opens With:”从“默认登录 shell”更改为 Command 和值“/bin/bash”。

Alternately, you can change your default shell by executing the following command at the command prompt:

或者,您可以通过在命令提示符下执行以下命令来更改默认 shell:

chsh -s bin/bash

After you do one of these, open a new shell window, and your .bash_profile should be sourced.

完成其中之一后,打开一个新的 shell 窗口,您的 .bash_profile 应该是来源。

回答by Steve Benner

It should be mentioned that bash will first look for a /etc/profilefile, as stated in the Bash man pages.

应该提到的是,bash 将首先查找/etc/profile文件,如 Bash 手册页中所述。

When bash is invoked as an interactive login shell, or as a non-inter- active shell with the --login option, it first reads and executes com- mands 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 选项来禁止这种行为。

回答by hong developer

You can use zshto fix the problem.

您可以使用zsh来解决问题。

The Z shell (also known as zsh) is a Unix shell that is built on top of bash(the default shell for macOS) with additional features. It's recommended to use zshover bash.

Z shell(也称为zsh)是一个 Unix shell,它构建在bash(macOS 的默认 shell)之上,具有附加功能。它建议使用zshbash.

Installation

安装

  1. Install zsh using Homebrew: $ brew install zsh
  2. Install Oh My Zsh: $ sh -c "$(curl -fsSL https://raw.githubusercontent.com/robbyrussell/oh-my-zsh/master/tools/install.sh)"
  3. Move to .bash_profilesetting .zshrcfile
  4. To apply the changes you make you need to either start new shell instance or run: source ~/.zshrc
  1. 使用 Homebrew 安装 zsh: $ brew install zsh
  2. 安装 Oh My Zsh: $ sh -c "$(curl -fsSL https://raw.githubusercontent.com/robbyrussell/oh-my-zsh/master/tools/install.sh)"
  3. 移动到.bash_profile设置.zshrc文件
  4. 要应用您所做的更改,您需要启动新的 shell 实例或运行: source ~/.zshrc

回答by Anshul

I solved by simply adding bash(in a newline) into ~/.bash_profile file.

我通过简单地将bash(在换行符中)添加到 ~/.bash_profile 文件中来解决。