Alias defined in .bash_profile not working in OS X

落花浮王杯 提交于 2019-12-01 03:15:55

问题


I have defined three aliases in my .bash_profile but my bash shell is not reading it. The aliases defined are not working in my terminal and I couldn't resolve this issue.

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

This line is defined inside the .bash_profile but it is not working in my shell.

Things I have tried so far:

  • I have created .bashrc file and defined the alias but it is also not working.

  • if I used source ~rsukla/.bash_profile then the alias are working fine but I want the alias permanently so I don't have to use source every time I open my shell

Any idea why the hell the alias are not working in when I define in .bash_profile?


回答1:


We still don't know why aliases are not loaded automatically.

Your aliases should be loaded from the .bash_profile. This is the default behaviour of bash in OS X.

mklement0 wrote more about the problem in their answer in this thread.

A hacky workaround

Open the Preferences of the Terminal. You specify the command you start your shell with:

Instead of manually sourcing your dotfiles every time you can specify which file you want to source when the shell opens. Here is the list of options for 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

You might consider using /bin/bash --rcfile alias_file_of_yours or something similar.

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

As a side note I do recommend you to check out goat. It lets you manage such cd aliases easily.

I use it and I wrote it.




回答2:


Assuming:

  • you use OS X's native terminal, Terminal.app, or popular alternative iTerm2
  • and bash is indeed your shell (as is the default on OS X)

then ~/.bash_profile should be loaded for every interactive shell, because both terminal programs create login shells by default.
Bash login shells source ~/.bash_profile, but not ~/.bashrc.

Note that this differs from most Linux distros, where a single login shell is executed on startup, and later interactive shells are non-login shells, which only load ~/.bashrc, not ~/.bash_profile.
A frequently seen technique to ensure that definitions are loaded in both login and non-login interactive shells is to place definitions in ~/.bashrc, and then source it from ~/.bash_profile, using the following line:
[[ -f ~/.bashrc ]] && . ~/.bashrc


You can create a login shell on demand by executing bash -l from an existing shell; if that loads your aliases, then the problem must be with what your default shell is and/or how your terminal program is configured.

  • echo $SHELL tells you what your default shell is.
  • if you use Terminal.app, Terminal > Preferences..., tab General, setting Shells open with tells you whether the default shell or a custom shell is being used.


来源:https://stackoverflow.com/questions/36484139/alias-defined-in-bash-profile-not-working-in-os-x

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!