Take the 2-minute tour ×
Ask Different is a question and answer site for power users of Apple hardware and software. It's 100% free, no registration required.

This question already has an answer here:

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?

share|improve this question

marked as duplicate by Mark, bmike Sep 23 at 22:56

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

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

2 Answers 2

up vote 7 down vote accepted

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.

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. – Chris Johnsen

For More information see the manual page for bash. Then look under the INVOCATION section.

man bash
share|improve this answer
    
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 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 at 2:27

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.

share|improve this answer
    
This is noted in the other answer - why are you unsure about these things? –  Mark Sep 23 at 21:56
    
Please read this very similar question: apple.stackexchange.com/q/12993/22003 and add there any improvment. –  daniel Azuelos Sep 24 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 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 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?". –  daniel Azuelos Sep 26 at 15:38

Not the answer you're looking for? Browse other questions tagged or ask your own question.