39

I added this line into my ~/.bashrc file.

alias myserver='ssh [email protected]'

However, when I open terminal and run myserver, terminal complains that

-bash: myserver: command not found

When I source my .bashrc file, however, (. .bashrc), the alias works.

Nonetheless, I do not want to source the file every time I open terminal for the alias to work. How do I make that happen?

David Faux
  • 2,035

2 Answers2

35

In short, you are putting your aliases in the wrong file .bashrc, that is why you need to keep running source to get the aliases working in any new login terminal instances.

From Chris Johnsen's comment at Why doesn't .bashrc run automatically?

By default, Terminal starts the shell via /usr/bin/login, which makes the shell a login shell. On every platform (not just Mac OS X) bash does not use .bashrc for login shells (only /etc/profile and the first of .bash_profile, .bash_login, .profile that exists and is readable). This is why “put source ~/.bashrc in your .bash_profile” is standard advice.

For more information, see the manual page for bash:

man bash

Then look under the INVOCATION section.

jaume
  • 15,010
MrDaniel
  • 12,742
  • Do I need to reboot in order to get things working? Because I added source ~/.bashrc to my .bash_profile, but I'm still facing the same issue... – Michiel Jan 07 '14 at 09:35
  • 4
    No in most cases you just need to open a new terminal window for the changes to take effect, the changes will only take effect in the newly opened window. – MrDaniel Jan 09 '14 at 02:27
14

You can always put

if [ -n "$BASH_VERSION" ] && [ -f $HOME/.bashrc ];then
    source $HOME/.bashrc
fi

into the file ~/.profile or ~/.bash_profile on mac I think.

Oooor, you could just put your stuff in .profile or .bash_profile.

Dylan
  • 489
  • This is noted in the other answer - why are you unsure about these things? – mmmmmm Sep 23 '14 at 21:56
  • Please read this very similar question: http://apple.stackexchange.com/q/12993/22003 and add there any improvment. – dan Sep 24 '14 at 06:47
  • @danielAzuelos Wait; do you want me to comment on the other page or update my answer based on the other question? – Dylan Sep 26 '14 at 15:11
  • @Mark I was just thinking in a very hypothetical sense - like what if someone's terminal process didn't know to source .profile? Their terminal probably would not start in the first place, but it is still possible.... like if someone was hacking around at their /usr/bin/login or something. – Dylan Sep 26 '14 at 15:13
  • → Dylan: please read this very similar question: http://apple.stackexchange.com/q/12993/22003 and you'll find an answer to your "maybe?". – dan Sep 26 '14 at 15:38
  • Oh; ok. While there's probably a 99.9999999999% chance that profile will be source it hypothetically could not be. I could take it out if you want me to. – Dylan Sep 26 '14 at 15:48