【问题标题】:How to load ~/.bash_profile when entering bash from within zsh?从 zsh 中输入 bash 时如何加载 ~/.bash_profile?
【发布时间】:2023-04-01 16:29:01
【问题描述】:

我已经使用 bash 两年了,只是尝试通过自制软件在我的 OS X 上切换到 zsh shell。我将我的默认(登录)shell 设置为 zsh,并确认它设置正确,因为当我启动终端时,它是默认使用的 zsh shell。

但是,当我尝试从 zsh 中进入 bash shell 时,它似乎没有加载 ~/.bash_profile,因为我无法使用别名运行我的命令,别名在我的 ~/.bash_profile 中定义,如 alias julia="~/juila/julia" 等。另外,提示不是我在文件中设置的,而是返回bash-3.2$

由于某些原因,当我将登录 shell 设置为 bash,并从 bash 中输入 zsh 时,~/.zshrc 被正确加载。

那么为什么我在 zsh 中运行 bash 时没有加载它?我的~/.bash_profile 符号链接到~/Dropbox/.bash_profile,以便与我的其他计算机同步。也许它会导致问题?

【问题讨论】:

  • 运行后您的别名是否可用source ~/.bash_profile

标签: bash macos shell zsh zshrc


【解决方案1】:

打开~/.zshrc,并在文件的最底部添加以下内容:

if [ -f ~/.bash_profile ]; then 
    . ~/.bash_profile;
fi

每次打开终端时,它都会加载~/.bash_profile 中定义的任何内容(如果文件存在)。这样,您可以保留 zsh 的自定义设置(颜色等)。您可以将自定义 shell 设置保存在 .bash_profile 文件中。

这比使用bash -lIMO 干净得多。

如果您更喜欢将设置放入 .bashrc.bash_login.profile ,您可以对它们执行相同的操作。


同样,您也可以将通用配置文件设置移动到单独的文件,即.my_common_profile,并将以下内容添加到.bash_profile.zshrc

if [ -f ~/.my_common_profile ]; then 
    . ~/.my_common_profile;
fi

【讨论】:

    【解决方案2】:

    交互式bash 读取您的~/.bash_profile(如果它是登录shell)或您的~/.bashrc(如果它不是登录shell)。

    典型的.bash_profile 将包含如下内容:

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

    所以.bashrc 可以包含由登录或非登录shell 执行的命令。

    如果您运行bash -l 而不仅仅是bash,它应该读取您的.bash_profile

    参考:https://www.gnu.org/software/bash/manual/html_node/Bash-Startup-Files.html

    【讨论】:

    • 谢谢。我在~/.bash_profile 的顶部定义了~/.bashrc 加载,但在~/.bash_profile 中定义了几乎所有设置,包括别名和环境变量,而不是~/.bashrc。我确认 bash -l 从 zsh 中成功加载它。
    • 我不知道为什么 oh-my-zsh 会更新,它会删除对 .zshrc 的所有更改。
    【解决方案3】:

    对于那些刚刚安装 zsh 并希望他们的 bash 别名在 zsh 上工作的人,请执行以下操作

    1. 像这样在vim中打开.zshrc文件

       vi ~/.zshrc
      
    2. 滚动到底部

    3. 点击“i”开启写入模式
    4. 告诉 zsh 在需要时从 bash_profile 加载项目,像这样
      source ~/.bash_profile
      
    5. 像这样写和退出
      :wq
      
    6. 像这样刷新你的 zsh
      source ~/.zshrc
      
      而已。现在,您在 .bash_profile 中保存的所有别名都可以在 zsh 中使用了。

    【讨论】:

      【解决方案4】:

      补充@Keith Thompson 的出色回答:

      ma​​cOS

      作为@chepner puts it succinctly(强调我的):

      在 OS X 中,bash 不用作初始 [启动时] 登录过程的一部分,并且 Terminal.app(或其他终端仿真器)进程存在于任何预先存在的 bash 会话之外,因此 每个新窗口 [或选项卡 - 阅读:交互式 bash shell](默认情况下)将自己视为新的登录会话

      因此,一些 OSX 用户只创建 ~/.bash_profile,而从不打扰 ~/.bashrc,因为所有交互式 bash shell 都是 login shell。 p>

      Linux

      在 Linux 上,情况通常颠倒bash 交互式创建的 shell 是 [interactive] NON-login shell,所以重要的是 ~/.bashrc

      因此,许多 Linux 用户只处理过~/.bashrc


      要维护 适用于两个平台的 bash 配置文件,请使用@Keith Thompson 提到的技术:

      • 将您的定义(别名、函数...)放入~/.bashrc
      • 将以下行添加到~/.bash_profile
      [[ -f ~/.bashrc ]] && . ~/.bashrc
      

      【讨论】:

      • 感谢您的出色跟进!你如何看待我只将if [ -f ~/.bashrc ]; then . ~/.bashrc; fi留在我的~/.bash_profile中,而将其他所有内容移至~/.bashrc的想法?
      • @Gardecolo:我认为这是个好主意(有一个很大程度上假设的警告:在启动期间创建bash 登录会话的平台上 - 如前所述,OSX 不是其中之一 - 你可以争辩说所有export 语句都应该进入~/.bash_profile,因为它们只需要执行一次,并且为每个新的bash shell 重复它们是不必要的——但实际上,我怀疑它很重要)。
      【解决方案5】:

      从 ~/.bash_profile 复制内容并将它们粘贴到 ~/.zshrc 文件的底部。

      【讨论】:

        【解决方案6】:

        对于 MacOs 上的 ZSH 用户,我最终选择了一条线。

        ~/.zshrc 的最底部我添加了以下行:

        bash -l
        

        它所做的只是加载 .bash_profile 设置(别名、函数、导出 $PATH、...)

        如果您决定摆脱 ZSH 并回到普通 BASH,您将完全恢复正常。

        【讨论】:

        • 这可行,但它会在 zsh 中创建另一个 bash shell,您会失去 zsh 的功能和颜色。
        • 代替这个,在~/.zshrc文件的最底部,添加source ~/.bash_profile
        【解决方案7】:

        如果这是您不常做的事情,或者只是不适合进行更改,您还可以在启动子 bash shell 后“获取”.bash_profile

        . ~/.bash_profile
        

        这将在该 shell 会话的整个生命周期中提取您在 .bash_profile 脚本中所做的设置。在大多数情况下,您应该能够重复该命令,因此这也是一种无需完全登录即可测试您所做的任何更改的简单方法,并且如果您在您对 .bash_profile 和/或 .bashrc 文件进行升级。

        【讨论】:

          【解决方案8】:

          适用于 macOS Big Sur(版本 11.5.2)

          • 打开.zshrc

            • 例如:sudo nano ~/.zshrc
          • 在文件末尾添加source ~/.bash_profile

          每次打开终端时,都会加载 bash 配置文件中的内容。

          【讨论】:

            【解决方案9】:

            最近我在 OS X 上安装了 oh-my-zsh 并将 zsh 设置为默认 shell 并遇到了同样的问题。
            我通过在.zshrc 文件末尾添加source ~/.bash_profile solved 这个问题。

            【讨论】:

              【解决方案10】:

              我正在使用一个名为 oh my zsh 的 zsh 框架,并且我已经尝试了此处列出的大多数解决方案,但它破坏了我的自定义主题的格式。但是,这些步骤对我有用。

              1. 在我的 .bash_profile

                底部添加新别名

                vi ~/.bash_profile

              2. 使 zsh.bash_profile

                加载项目

                source ~/.bash_profile

              3. 刷新zsh

                source ~/.zshrc

              4. 重启 OSX 终端应用

              5. 试试你的新别名!

              【讨论】:

                【解决方案11】:

                如果您想“以个人资料为中心”,您可以创建 .profile 作为单一事实来源,然后从 .bash_profile.zprofile 加载它。

                .profile

                export PATH="/usr/local/opt/python/libexec/bin:$PATH"
                # etc., etc.
                

                .bash_profile.zprofile

                if [ -f ~/.profile ]; then 
                    . ~/.profile;
                fi
                

                我发现这有助于 bash 脚本找到正确的 PATH 等,并帮助我将配置保存在一个地方。

                【讨论】:

                  猜你喜欢
                  • 1970-01-01
                  • 1970-01-01
                  • 2011-06-04
                  • 1970-01-01
                  • 2021-09-08
                  • 2014-05-30
                  • 1970-01-01
                  • 2019-10-27
                  • 1970-01-01
                  相关资源
                  最近更新 更多