views:

3561

answers:

5

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 has a comment line that says:

for non-login shell

and

.bash_profile has a comment

for login shell

so where should aliases be written in? how come we separate login shell and non-login shell?

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

+1  A: 

bash_profile is loaded for a "login shell". I am not sure what that would be on OS X but in linux that is either X11 or a virtual terminal.

bashrc is loaded everytime you run bash. That is where you should put stuff you want loaded whenever you open a new Terminal.app window.

I personally put everything in bashrc so that I don't have to restart the app for changes to take effect.

Sionide21
+9  A: 

The reason you separate the login and non login shell is because the .bashrc is reloaded every time you start a new copy of bash. The profile is loaded only when you either login, or use the appropriate flag to tell bash to ast as a login shell.

What I do is

  • put my PATH set up into a .profile file (because I sometimes use other shells)
  • Put my bash aliases and functions into my .bashrc
  • Then use this:

.bash_profile:

#!/bin/bash
#
# CRM .bash_profile Time-stamp: "2008-12-07 19:42"
#
# echo "Loading ${HOME}/.bash_profile"
source ~/.profile # Get the paths
source ~/.bashrc  # get aliases
#
#- end

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

source ~/.bashrc

which loads the .bashrc as if you had typed the commends directly to bash.

Charlie Martin
+2  A: 

From the bash manpage:

When bash is invoked as an interactive login shell, or as a non-interactive shell with the --login option, it first reads and executes commands from the file /etc/profile, if that file exists. After reading that file, it looks for ~/.bash_profile, ~/.bash_login, and ~/.profile, in that order, and reads and executes commands from the first one that exists and is readable. The --noprofile option may be used when the shell is started to inhibit this behavior.

When a login shell exits, bash reads and executes commands from the file ~/.bash_logout, if it exists.

When an interactive shell that is not a login shell is started, bash reads and executes commands from ~/.bashrc, if that file exists. This may be inhibited by using the --norc option. The --rcfile file option will force bash to read and execute commands from file instead of ~/.bashrc.

Thus, if you want to get the same behavior for both login shells and interactive non-login shells, you should put all of your commands in either .bashrc or .bash_profile, and then have the other file source the first one.

Adam Rosenfield
+2  A: 

Check out http://mywiki.wooledge.org/DotFiles for an excellent resource on the topic aside from man bash.

Summary:

  • You only log in once, and that's when ~/.bash_profile or ~/.profile is read and executed. Since everything you run from your login shell inherits the login shell's environment, you should put all your environment variables in there. Like LESS, PATH, MANPATH, LC_*, ... For an example, see: My .profile
  • Once you log in, you can run several more shells. Imagine logging in, running X, and in X starting a few terminals with bash shells. That means your login shell started X, which inherited your login shell's environment variables, which started your terminals, which started your non-login bash shells. Your environment variables were passed along in the whole chain, so your non-login shells don't need to load them anymore. Non-login shells only execute ~/.bashrc, not /.profile or ~/.bash_profile, for this exact reason, so in there define everything that only applies to bash. That's functions, aliases, bash-only variables like HISTSIZE (this is not an environment variable, don't export it!), shell options with set and shopt, etc. For an example, see: My .bashrc
  • Now, as part of UNIX peculiarity, a login-shell does NOT execute ~/.bashrc but only ~/.profile or ~/.bash_profile, so you should source that one manually from the latter. You'll see me do that in my ~/.profile too: source ~/.bashrc.
lhunath
A: 

what is the diffrence between .bashrc and .profile files what re the drawback and advantages of each

chanti