28

I have a couple of bash scripts that I want to make sure runs by default and I'm currently storing them in ~/.profile on my mac. Is that the wrong place to be storing them? I've heard of others and tried them (like ~/.bashrc, ~/.bash_profile, etc), but they don't seem to be working.

What is the difference between all of these and which one do I put the scripts in so that it configures on runtime and I don't have to call $ source ~/.profile every time I open the terminal?

locoboy
  • 38,002
  • 70
  • 184
  • 260
  • See also http://stackoverflow.com/questions/415403/whats-the-difference-between-bashrc-bash-profile-and-environment – Nakilon Aug 04 '13 at 18:54

5 Answers5

18

If both ~/.bash_profile and ~/.profile exist, bash only reads ~/.bash_profile when it is invoked as an interactive login shell.

https://www.gnu.org/s/bash/manual/html_node/Bash-Startup-Files.html:

Invoked as an interactive login shell, or with --login

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.

[...]

Invoked as an interactive non-login shell

When an interactive shell that is not a login shell is started, Bash reads and executes commands from ~/.bashrc, if that file exists.

~/.profile is also used by other shells.

Terminal and iTerm open new shells as login shells by default (by executing something like login -pf $USER), but many GNU/Linux terminal applications open new shells as non-login shells. OS X users often use ~/.bash_profile instead of ~/.bashrc.

Community
  • 1
  • 1
Lri
  • 26,768
  • 8
  • 84
  • 82
9
                     +-----------------+
                     |                 |
interactive shell -->|  ~/.bashrc      |
                     |                 |
                     +-----------------+

interactive shell will source ~/.bashrc automatically.

Take a look at Should the .bashrc in the home directory load automatically?

Community
  • 1
  • 1
kev
  • 155,172
  • 47
  • 273
  • 272
  • I'll try this. What is the difference between the login shell and the others? – locoboy Apr 25 '12 at 18:10
  • 1
    An interactive shell is one you run inside a terminal; that is, both stdin and stdout point to the terminal's file descriptors. 99% of the shells you work with will be run as an interactive shell. – David Souther Apr 25 '12 at 18:12
  • How do I make sure that I'm running interactive and not another one? It seems like I'm running another one. – locoboy Apr 25 '12 at 18:13
  • 1
    @locoboy. Append `source ~/.bashrc` to `~/.bashrc_profile` manually. – kev Apr 26 '12 at 00:46
  • 10
    I'm not sure that the ASCII box-and-arrow diagram is helpful. – Keith Thompson Aug 04 '13 at 21:48
9

I did these to rectify the problem:

cat .bash_profile >> .profile
rm .bash_profile

the alternative is:

echo "source ~/.profile" >> .bash_profile
Nakilon
  • 34,866
  • 14
  • 107
  • 142
user1970485
  • 91
  • 1
  • 1
6

Make sure if you do source ~/.profile in your .bashrc that you comment out or remove any commands (in .profile) to call or source .bashrc in your .profile or it will loop forever and you will never get a prompt.

IKavanagh
  • 6,089
  • 11
  • 42
  • 47
D Jackson
  • 61
  • 1
  • 1
3

Different setups of bash will automatically source different files depending on their configuration. The nearly universal file that is always sourced is ~/.bashrc - this is a bash core thing that it will load this file. In that file, you should add your line to source ~/.profile and you'll be good to go!

-Edit-

From my Linux and my colleague's Mac:

$ echo "echo hello" >> ~/.profile
$ echo "source ~/.profile" >> ~/.bashrc
$ bash
Hello
$ 
David Souther
  • 8,125
  • 2
  • 36
  • 53
  • I double-checked the solution on a colleague's mac, and posted. At this point, can you provide any insight to what is happening on your machine? If you could run and post ls -l ~/.bashrc, ls -l ~/.profile, cat ~/.bashrc, cat ~/.profile – David Souther Apr 25 '12 at 17:59
  • 1
    .bashrc is only sourced if the shell is interactive and *not* a login shell. By convention, .bashrc is called from `~/.bash_login` but this ain't necessarily so... so you can't count on .bashrc being called. See the link in @kev's post. – Barton Chittenden Apr 25 '12 at 18:02
  • I guess I still need more clarification- the impression from the question is that @locoboy is running this from terminal in osx, which would imply he's indeed using an interactive shell. – David Souther Apr 25 '12 at 18:08
  • Or, he should move all his scripts from ~/.profile to ~/.bashrc. I had considered posting that answer, but thought this to be more reasonable. – David Souther Apr 25 '12 at 18:10
  • He might run them from a terminal in OSX, OTOH, if he logged in to the same box via ssh, he would then be launching bash as a login shell, therefore .bashrc would not run, unless .bashrc was called by .bash_login. – Barton Chittenden Apr 25 '12 at 19:45
  • "Learning the bash Shell" by Newham & Rosenblatt covers the whole bash startup process. Their coverage is the second section of Chapter 3 in my copy of the 3rd edition. ".bashrc" typically doesn't run for a login shell, only for sub-shells. – Mark Apr 25 '12 at 20:31
  • @BartonChittenden That is very possible- in which case, we need a bit more information on what the problem is before downvoting answers. – David Souther Apr 26 '12 at 23:01