136

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.

codeforester
  • 28,846
  • 11
  • 78
  • 104
nonopolarity
  • 130,775
  • 117
  • 415
  • 675
  • 13
    Oh, this is dumb. This question has been around for almost two years. it's NOT an exact duplicate, since this one asks for prescriptive suggestions (what should be done). – Charlie Martin Feb 27 '12 at 03:06

4 Answers4

177

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.

Maggyero
  • 3,120
  • 2
  • 24
  • 43
Charlie Martin
  • 103,438
  • 22
  • 180
  • 253
  • 1
    It is supposed to be the other way round: `.bash_profile` sources `.bashrc`, and you use a non-login shell after logging in the first time. – Ioannis Filippidis Sep 09 '14 at 00:00
  • 8
    Go read what I said again. – Charlie Martin Sep 09 '14 at 23:36
  • Very nice explanation. Just wonder what it means exactly "bash loads your .bashrc when it starts". Do you mean when the machine starts or when the terminal start? – Andrea Moro Nov 03 '14 at 10:40
  • 1
    whenever a bash process starts. Say I do: $ bash $ bash $ bash then I have four `bash` processes running. .bashrc is loaded by each process. – Charlie Martin Nov 12 '14 at 21:10
  • That's just three calls to bash, so wouldn't that just make three bash processes? And doesn't one bash process have to terminate before the next one starts, or do they run in parallel for some reason? – HelloGoodbye Jun 17 '15 at 21:06
  • @HelloGoodbye there's one bash process executing that command lines: to start. Think of it as bash_0, then it starts bash_1, which starts bash_2, which starts bash_3, for a total of 4. – Charlie Martin Jun 18 '15 at 00:02
  • Ah, that's true. But still, by `$ bash $ bash $ bash`, do you mean that you run the bash command in three separate commands, so one has to terminate before the next one starts and they don't run in parallel? Or is that maybe unimportant for this example? – HelloGoodbye Jun 18 '15 at 07:14
  • There should have been newlines between them but comments wouldn't do that. There are four shells running concurrently (not in parallel, strictly) but three of them are suspended waiting for a child to terminate. Now, for a good time, try `$ bash & bash & bash &`. – Charlie Martin Jun 18 '15 at 09:52
  • @CharlieMartin nice answer, I'm gonna reference this on my blog. Bookmarked as well. – Dio Phung Dec 04 '16 at 21:55
  • @DioPhung Thanks! – Charlie Martin Dec 04 '16 at 21:56
  • @CharlieMartin this is ancient and if you are still using this method maybe you could explain what the commented-out lines do and why they are commented out? Should/could these be removed for someone who is following your example? Thanks! – John Aug 27 '20 at 20:08
  • 1
    @CharlieMartin I would also like to point out that if you open another shell and use `login` to login as a different user - having the commands `source ~/.profile` and `source ~/.bashrc` will not source from the newly logged in user's home folder but the home folder of the user you signed into your computer with. Maybe there is another solution than using `~/` – John Aug 27 '20 at 22:07
133

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
  • 103,030
  • 16
  • 63
  • 74
  • 8
    This answer deserves way more upvotes. Right now I came back looking things up for the fourth time. – sjas Jan 06 '13 at 00:08
  • Also see: https://blog.flowblok.id.au/2013-02/shell-startup-scripts.html – codeforester Sep 07 '18 at 18:05
  • Following 2 URLS -Cleared my Years of Confusion https://linuxize.com/post/bashrc-vs-bash-profile/ https://medium.com/@abhinavkorpal/bash-profile-vs-bashrc-c52534a787d3 – Karan Kaw Aug 21 '20 at 04:59
13

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
  • 360,316
  • 93
  • 484
  • 571
4

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

.bashrc is loaded every time 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 application for changes to take effect.

Sionide21
  • 2,193
  • 2
  • 21
  • 30