在 .bash_profile 中定义的别名在 OS X 中不起作用

Alias defined in .bash_profile not working in OS X

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

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

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

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

知道为什么当我在 .bash_profile 中定义时 alias 不起作用吗?

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

您的别名应该从 .bash_profile 加载。这是 bash 在 OS X.

中的默认行为

mklement0 wrote more about the problem in .

一个 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 别名。

我用我写

假设:

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

然后 ~/.bash_profile 应该 为每个交互 shell 加载,因为 both终端程序默认创建 login shells.
Bash 登录 shell 来源 ~/.bash_profile,但不是 ~/.bashrc

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


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

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