26

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

alias myserver='ssh davidfaux@davidfaux.com'

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?

  • 3
    This comment explains the reason for this. That question tells you how to fix it. – ughoavgfhw Mar 4 '12 at 19:35
  • Thanks a lot! I did not know that starting the shell executes those files in order. Indeed, adding . .bashrc into .bash_profile works. – David Faux Mar 4 '12 at 19:43
  • @ughoavgfhw can/should you change your comment to an answer? That way it can be marked as an answer. – soxman Mar 5 '12 at 14:14
25

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.

  • 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 7 '14 at 9:35
  • 1
    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 9 '14 at 2:27
11

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.

  • This is noted in the other answer - why are you unsure about these things? – user151019 Sep 23 '14 at 21:56
  • Please read this very similar question: apple.stackexchange.com/q/12993/22003 and add there any improvment. – dan Sep 24 '14 at 6: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: apple.stackexchange.com/q/12993/22003 and you'll find an answer to your "maybe?". – dan Sep 26 '14 at 15:38

Not the answer you're looking for? Browse other questions tagged .