Search code examples
bashshellenvironment

Should aliases go in .bashrc or .bash_profile?


Possible Duplicate: What's the difference between .bashrc, .bash_profile, and .environment?

It seems that if I use

alias ls='ls -F'

inside of .bashrc on Mac OS X, then the newly created shell will not have that alias. I need to type bash again and that alias will be in effect.

And if I log into Linux on the hosting company, the .bashrc file has a comment line that says:

For non-login shell

and the .bash_profile file has a comment that says

for login shell

So where should aliases be written in? How come we separate the login shell and non-login shell?

Some webpage say use .bash_aliases, but it doesn't work on Mac OS X, it seems.


Solution

  • The reason you separate the login and non-login shell is because the .bashrc file is reloaded every time you start a new copy of Bash. The .profile file is loaded only when you either log in or use the appropriate flag to tell Bash to act as a login shell.

    Personally,

    • I put my PATH setup into a .profile file (because I sometimes use other shells);
    • I put my Bash aliases and functions into my .bashrc file;
    • I put this

      #!/bin/bash
      #
      # CRM .bash_profile Time-stamp: "2008-12-07 19:42"
      #
      # echo "Loading ${HOME}/.bash_profile"
      source ~/.profile # get my PATH setup
      source ~/.bashrc  # get my Bash aliases
      

      in my .bash_profile file.

    Oh, and the reason you need to type bash again to get the new alias is that Bash loads your .bashrc file when it starts but it doesn't reload it unless you tell it to. You can reload the .bashrc file (and not need a second shell) by typing

    source ~/.bashrc
    

    which loads the .bashrc file as if you had typed the commands directly to Bash.