Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

About .bash_profile, .bashrc, and where should alias be written in? [duplicate]

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.

like image 814
nonopolarity Avatar asked May 24 '09 02:05

nonopolarity


People also ask

Should alias go in bashrc or bash_profile?

bashrc or in ~/. bash_aliases but it should not go in . profile . If the variables are only ever needed in shells where the aliases (which expand to commands that use them) are available, then it is actually fine to put them in the same place as the aliases.

Can I put alias in bash_profile?

To make the alias persistent you need to declare it in the ~/. bash_profile or ~/. bashrc file. The aliases should be named in a way that is easy to remember.

What goes in bash_profile vs bashrc?

Answer: . bash_profile is executed for login shells, while . bashrc is executed for interactive non-login shells. When you login (type username and password) via console, either sitting at the machine, or remotely via ssh: .


1 Answers

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.

like image 188
Charlie Martin Avatar answered Oct 21 '22 23:10

Charlie Martin