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

问题描述 投票:5回答:2

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

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

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

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

  • 我创建了.bashrc文件并定义了别名,但它也无效。
  • 如果我使用source ~rsukla/.bash_profile然后别名工作正常但我想永久别名所以我不必每次打开我的shell都使用source

当我在alias中定义时,任何想法为什么.bash_profile不工作?

macos bash shell .bash-profile
2个回答
1
投票

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

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

mklement0their answer in this thread写了更多关于这个问题的文章。

A hacky workaround

打开终端的首选项。您可以使用以下命令指定启动shell的命令:

terminal preferences

每次在shell打开时指定要提供的文件时,不必手动获取dotfiles。以下是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或类似的东西。

Go for goat if you need aliases using the cd command.

作为附注,我建议你看看goat。它可以让你轻松管理这样的cd别名。

我用它然后写了它。


4
投票

假设:

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

然后应为每个交互式shell加载~/.bash_profile,因为默认情况下两个终端程序都会创建登录shell。 Bash登录shell源~/.bash_profile,但不是~/.bashrc

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


您可以通过从现有shell执行bash -l来按需创建登录shell;如果加载别名,则问题必须与您的默认shell和/或终端程序的配置方式有关。

  • echo $SHELL告诉你你的默认shell是什么。
  • 如果您使用Terminal.app,Terminal > Preferences...,tab General,设置Shells open with会告诉您是使用默认shell还是自定义shell。
© www.soinside.com 2019 - 2024. All rights reserved.