【问题标题】:Alias defined in .bash_profile not working in OS X.bash_profile 中定义的别名在 OS X 中不起作用
【发布时间】:2016-04-07 18:15:10
【问题描述】:

我在.bash_profile 中定义了三个别名,但我的 bash shell 没有读取它。定义的别名在我的终端中不起作用,我无法解决此问题。

alias handybook="cd /Users/rsukla/development/repos/handybook/"

这一行是在.bash_profile 中定义的,但它在我的shell 中不起作用。

到目前为止我尝试过的事情:

  • 我创建了.bashrc 文件并定义了别名,但它也不起作用。

  • 如果我使用了 source ~rsukla/.bash_profile,那么别名工作正常,但我想要永久使用别名,这样我就不必每次打开 shell 时都使用 source

知道为什么当我在.bash_profile 中定义时alias 无法正常工作吗?

【问题讨论】:

  • 您可以考虑使用goat 来管理您的快捷方式别名。
  • alias 唯一没有采购的命令吗?环境变量呢?
  • 我认为我的 bash shell 没有读取 ~/.bash_profile。但是让我检查一下其他命令

标签: macos bash shell .bash-profile


【解决方案1】:

假设:

  • 您使用 OS X 的本机终端 Terminal.app 或流行的替代品 iTerm2
  • bash 确实是你的 shell(OS X 上的默认设置)

那么~/.bash_profile应该为每个交互式shell加载,因为两个终端程序默认创建loginshell强>.
Bash login shell 源 ~/.bash_profile,但不是 ~/.bashrc

请注意,这不同于大多数 Linux 发行版,其中 单一 登录 shell 在启动时执行 em> 和后来的交互式 shell 是 非登录 shell,它只加载 ~/.bashrc,而不是 ~/.bash_profile
一种常见的确保在登录和非登录交互式 shell 中加载定义的技术是将定义放在 ~/.bashrc,然后从 ~/.bash_profile 获取它,使用以下行:[[ -f ~/.bashrc ]] && . ~/.bashrc


您可以通过在现有 shell 中执行 bash -l 来按需创建登录 shell;如果这会加载您的别名,那么问题一定出在您的默认 shell 是什么和/或您的终端程序是如何配置的。

  • echo $SHELL 告诉你你的默认 shell 是什么。
  • 如果您使用 Terminal.app,Terminal > Preferences...,选项卡 General,设置 Shells open with 会告诉您是使用默认 shell 还是自定义 shell。

【讨论】:

    【解决方案2】:

    我们仍然不知道为什么没有自动加载别名。

    您的别名应该从.bash_profile 加载。这是 bash 在 OS X 中的默认行为。

    mklement0their answer in this thread 中写了更多关于该问题的信息。

    一个 hacky 解决方法

    打开终端的首选项。您指定启动 shell 的命令:

    每次您都可以在 shell 打开时指定要获取的文件,而不是手动获取您的点文件。以下是bash 的选项列表:

    Usage:  bash [GNU long option] [option] ...
        bash [GNU long option] [option] script-file ...
    GNU long options:
        --debug
        --debugger
        --dump-po-strings
        --dump-strings
        --help
        --init-file
        --login
        --noediting
        --noprofile
        --norc
        --posix
        --protected
        --rcfile
        --restricted
        --verbose
        --version
        --wordexp
    Shell options:
        -irsD or -c command or -O shopt_option      (invocation only)
        -abefhkmnptuvxBCHP or -o option
    

    您可以考虑使用/bin/bash --rcfile alias_file_of_yours 或类似的东西。

    如果您需要使用cd 命令的别名,请使用goat

    作为旁注,我建议您查看goat。它可以让您轻松管理此类cd 别名。

    我用过,我写过。

    【讨论】:

    • 我无法永久定义别名,但我可以在每次启动新的 bash shell 时使用source ~/.bashrc 来加载我的别名。适合我
    猜你喜欢
    • 2020-04-10
    • 2017-04-30
    • 1970-01-01
    • 2010-10-31
    • 1970-01-01
    • 2017-07-10
    • 1970-01-01
    • 2023-03-14
    • 2015-12-17
    相关资源
    最近更新 更多