279

I put some alias commands in my .bashrc file, so that they might be loaded everytime I open a new Terminal window. Yet this doesn't happen.

I have to select run script: in the Terminal>Preferences>"MyDefaultTheme">Shell prefpane and add: source .bashrc && clear for it to work...

This seems odd since many tutorials only say you just have to add to the .bashrc file and all is good.

Note that I don't run in bash when using the terminal, I like the other (default) one (don't know what it is) better, because it show me where I am all the time eg:

>>d54c6b47b:~ romeo$

in stead of:

>>bash$

In bash all is loaded as should be.

So my question is, why didn't my .bashrc file load automatically and did I have to add the option to call it everytime?

Also some tutorial told me to do something like:

$alias la=’ls -la’ >> ~/.bashrc

which should write the alias to my .bashrc, this doesn't work either...

Note that I'm a UNIX novice, so be gentle.

7
  • I think this is very close to apple.stackexchange.com/questions/7984/….
    – boehj
    Apr 27, 2011 at 21:13
  • 1
    If you are instead using the default zsh shell (not i.e: bash) configured in 2019+ OS X (currently Catalina), see apple.stackexchange.com/a/338622/192005
    – cellepo
    Jun 22, 2020 at 23:48
  • @mmmmmm I've rolled back you edit. The question is clearly about bash, and there even are some recent answers pointing that zsh works differently.
    – nohillside
    Jul 22, 2020 at 9:41
  • @nohillside those answers are why I edited. In effect catalina makes the high voted answers wrong but only for catalina. Ie the correct answer rather depends on the os and we often ask what os. Also we might see this is about bash but the new answers show that it is seen about startup scripts and where to set aliases in macos and not looking to see which shell is being used.
    – mmmmmm
    Jul 22, 2020 at 10:31
  • @mmmmmm The answer depends on the shell used, not directly on the OS. I agree that not everybody will be aware of this, but IMHO a new question linking to this one and claiming „it didn’t work“ would be better. The new answers here basically say „maybe you ain‘t using bash, try zsh stuff“.
    – nohillside
    Jul 22, 2020 at 10:45

14 Answers 14

125

Just put that in your .profile file from your home dir and it should work the next time you start a new shell or after you run source ~/.profile

This link clearly states the order in which the startup files are read and loaded by the shell: http://hayne.net/MacDev/Notes/unixFAQ.html#shellStartup

4
  • accepted this one because it is has a solution (and it was the first of two near identical posts).
    – romeovs
    Apr 28, 2011 at 5:15
  • 59
    Down-voted. I recommend against following this advice. The issue is that Terminal creates login shells, and Bash login shells only run the login startup script, not ~/.bashrc. However, the solution isn't to simply place your .bashrc content into the login startup file, because these two files are intended to perform different types of setup. Instead, the canonical setup for Bash is to have your ~/.bash_profile source your ~/.bashrc at some appropriate point in the script (usually last).
    – Chris Page
    Dec 7, 2015 at 10:31
  • I see no other identical post this just says put in the sh file
    – mmmmmm
    Apr 4, 2016 at 14:26
  • 1
    If you are instead using the default zsh shell (not i.e: bash) configured in 2019+ OS X (currently Catalina), see apple.stackexchange.com/a/338622/192005
    – cellepo
    Jun 22, 2020 at 23:53
289

Been there, done that. What I came aware of, OS X doesn't read .bashrc file on bash start. Instead, it reads the following files (in the following order):

  1. /etc/profile
  2. ~/.bash_profile
  3. ~/.bash_login
  4. ~/.profile

See also Chris Johnsen's informative and useful comment:

By default, Terminal starts the shell via /usr/bin/login, which makes the shell a login shell. On every platform (not just Mac OS X) bash does not use .bashrc for login shells (only /etc/profile and the first of .bash_profile, .bash_login, .profile that exists and is readable). This is why "put source ~/.bashrc in your .bash_profile" is standard advice

I usually just put the things that I'd normally put in ~/.bashrc to ~/.profile — has worked so far like a charm.

14
  • 57
    By default, Terminal starts the shell via /usr/bin/login, which makes the shell a login shell. On every platform (not just Mac OS X) bash does not use .bashrc for login shells (only /etc/profile and the first of .bash_profile, .bash_login, .profile that exists and is readable). This is why “put source ~/.bashrc in your .bash_profile” is standard advice. Apr 28, 2011 at 4:21
  • 7
    Re "I usually just put the things that I'd normally put in ~/.bashrc to ~/.profile": For maximum compatibility I recommend you follow Chris Johnsen's advice and place anything that you want to apply to non-login shells in ~/.bashrc and add "source ~/.bashrc" to your ~/.bash_profile to run it for login shells. Otherwise, it won't work for sub-shells, or if Terminal changes to create non-login shells, or if you ever use xterm or another terminal program that create non-login shells by default, or if you might want to use the same setup on another OS.
    – Chris Page
    Aug 14, 2011 at 1:35
  • 4
    On a related note, on Mac OS X you should consider having ~/.bashrc run /etc/bashrc to pick up global behaviors. In particular, there's code in /etc/bashrc to update the working directory at each prompt, which is what enables Terminal to display the proxy icon, create new terminals at the same directory, and restore the working directory for Resume and Window Groups.
    – Chris Page
    Aug 14, 2011 at 1:38
  • For me, sourcing .bashrc from .bash_profile didn't work. But sourcing .bashrc / .bash_profile from .profile worked. Jan 9, 2013 at 8:02
  • 5
    Nice answer but a bit misinformed. I believe the bash terminal loads only one of the ~/.bash_profile, ~/.profile, ~/.bash_login, never all of the them. You can run a simple test by creating the 3 files. It looks like the bash terminal has the preference of ~/.bash_profile > ~/.bash_login > ~/.profile. If anyone of them exists, the ones of less preference are skipped. That's why sometimes it is recommended to also source ~/.profile in ~/.bash_profile.
    – neurite
    Nov 30, 2015 at 22:32
34

I put everything into ~/.bashrc and just source ~/.bashrc in .profile.

This allows screen and xterm (and i guess tmux) sessions to inherit my environment as non-login sessions only run .bashrc, whereas login sessions (eg terminal or iTerm) only run .profile.

2
  • 1
    This is my approach. This way I can maintain a .bashrc that works on both systems... and I can put any mac specific stuff or stuff like aliases that are only for work (where I use the mac the most) in my .bash_profile file. May 27, 2014 at 10:28
  • 6
    Note that .profile is common to several shells and should only contain POSIX-compatible sh code. If it sources .bashrc it arguably contains Bash-specific code (the existence of the .bashrc file is Bash-specific). I recommend renaming your .profile to .bash_profile since you're using it in a Bash-specific way. Or, put generic code in .profile and create a .bash_profile that sources .profile and .bashrc.
    – Chris Page
    Dec 7, 2015 at 10:43
24

There are two scenarios:

  1. You are using Linux
  2. You are using Mac Os X

For both you want to source ~/.bashrc in your profile that gets loaded, or sourced, when your shell for your terminal starts.


LINUX

In Linux, ~/.profile is automatically source in your shell when it starts. So, if you go to your terminal and type cd ~; ls -A, you will see all of your files and directories in your home directory (/home/usrname/). You should see a file called .profile. This is the file that is automatically "sourced" when you start the terminal.

If you want to add aliases and functions to ~/.bashrc (which is what I do), then you should (inside of ~/.profile add an if statement that checks if ~/.bashrc is a non-empty file and then source it.

To check if your ~/.profile already does this enter nano ~/.profile. This will open it up in a text editor (you can use gedit if you know you have it or even vim if you know how to use it). You may get an empty text file (you shouldn't), but if you do simply proceed. If you do not see a line anywhere that says source ~/.bashrc, then enter the following lines somewhere (put it in a reasonable place like at the end or beginning and not in the middle of an if statement):

if [ -s ~/.bashrc ]; then
    source ~/.bashrc;
fi

This checks if ~/.bashrc is a non-empty file (with if [ -s ~/.bashrc ]), and if it is such, it sources it. Pretty simple. Now, you can add any valid alias, function, variable, etc to ~/.bashrc.


MAC OS X

By default (prior to 2019 Catalina), Mac OS X sources ~/.bash_profile. To be able to add things to ~/.bashrc (which is, again, what I do in OS X, as well), then you follow the same procedure as linux. In the terminal enter cd ~; nano .bash_profile. Check for a line that says source ~/.bashrc and if it's not there add the if statement above (if [ -s ~/.bashrc ]; then source ~/.bashrc; fi).


NOTES

When writing an if statement, be sure to leave spaces between basically everything (for example if [ -s ~/.bashrc]; then echo "found"; fi does not work because there is no space beween ~/.bashrc and ] - the interpreter will think this is one word).

If you want to figure out how to check for stuff in an if statement better, go to terminal and enter man \[; this essentially gives you the run-down of an if statement. For example, if [ -f /path/to/file ]; then echo "it's a file"; else echo "not a file"; fi checks if /path/to/file is a file and if it is prints to stdout accordingly. I always reference this as well for easier-on-the-eyes and quick reference: 7.2. File test operators.

I hope this helps. I remember how confused I was when I started all of this stuff (which wasn't too long ago); so, good luck with your future UNIX endeavours!

2
  • On linux, .profile is only sourced if your shell is a login shell, otherwise .bashrc is sourced
    – smac89
    Aug 14, 2017 at 16:39
  • 1
    If you are instead using the default zsh shell (not i.e: bash) configured in 2019+ OS X (currently Catalina), see apple.stackexchange.com/a/338622/192005
    – cellepo
    Jun 22, 2020 at 23:54
21

By the way, if you have activated ZSH, it replaces bash!

So all those ~/.bashrc files don't get loaded.

You have to set your aliases in ~/.zshrc instead.

6
  • Thank you! I don't know why this had a downvote. Everything else wasn't working for me and I didn't think that ZSH was the problem.
    – WaltersGE1
    Oct 21, 2019 at 15:18
  • 2
    And on 2019+ Mac OS X (Catalina currently), zsh is the default (no activation necessary) shell (instead of bash) ;)
    – cellepo
    Jun 22, 2020 at 23:43
  • tnq :) 01234567 Oct 1, 2020 at 3:10
  • my man..........
    – om471987
    Dec 3, 2020 at 19:35
  • 2022, ohhh, man!
    – jared
    Jul 21, 2022 at 17:26
11

Sourcing .profile in .bash_profile did it for me

echo 'source ~/.profile' >> ~/.bash_profile
5
  • 2
    Sourcing .profile in .bash_profile is a great thing to do, but that alone wouldn't cause .bashrc to be run by login shells. Your .profile must've already sourced .bashrc, which could cause problems if you ever use a shell other than bash. Most Mac users never will, but if you do for some reason, remember this. :)
    – Terry N
    Mar 19, 2014 at 16:56
  • To expand on what @TerryN said: be sure to source .bashrc from your .bash_profile and not from .profile. .bash_profile is Bash-specific and should source the other two.
    – Chris Page
    Dec 7, 2015 at 10:46
  • 1
    In 2019+ (Mac OS X Caltalina installs), the default shell is zsh. It does not source .bash* files automatically and starts with .profile.
    – ingyhere
    May 30, 2020 at 4:11
  • 1
    Yes, osX Catalina defaults to zsh - but @ingyhere are you sure that sourcing starts with ~/.profile? It seemed in my case (on my Catalina) it did not, and I have instead been successfully using just ~/.zshrc (per apple.stackexchange.com/a/338622/192005).
    – cellepo
    Jun 22, 2020 at 23:38
  • 1
    @cellepo I think it depends on whether there is a ~/.zprofile. This answer cites the order from docs for zsh. Also, ~/.profile seems to be loaded by a number of shells, so it appears more universal.
    – ingyhere
    Jun 23, 2020 at 22:14
6

I found that after installing rvm (auto-installer, no manual edits) it had created a ~/.bash_login file for itself, where I previously never had one.

However, this mean that my ~/.profile setups and aliases no longer got loaded! Lots of shortcuts disappeared. I thought they ran sequentially, not exclusively :-/

I added

. ~/.profile 

to ~/.bash_login to chain things as I expected.

2
  • +1, this is situation I had. I found that either .bash_login or .bash_profile will prevent .profile from being executed... No idea why bash feels the need to have so many conflicting startup files.
    – RichVel
    Mar 13, 2013 at 21:21
  • 1
    .profile is common to several different shells and should only contain POSIX-compatible sh code. .bash_profile is Bash-specific and can contain Bash-specific code. In general, if you're using Bash you should prefer .bash_profile unless you are careful to ensure that your startup code does not contain anything that depends on Bash. Whatever program created .bash_login probably did so because it contains Bash-specific code, but it should have included code to source .profile if it exists.
    – Chris Page
    Dec 7, 2015 at 10:39
3

Putting...

[[ -s "$HOME/.rvm/scripts/rvm" ]] && . "$HOME/.rvm/scripts/rvm" # Load RVM function

...in ~/.bashrc, and then source ~/.bashrc into my ~/.bash_profile didn't work for the Terminal program that comes installed.

But then I went to the menu drop-down Edit > Profile Preferences, Title and Command tab, and selected Run command as a login shell from the command section of that page.

After doing so, starting a new terminal shows the desired result when I type

 type rvm|head -1

i.e. "rvm is a function". It did not do that until I made this change, and it's the only change I made after the .bash_profile and .bashrc changes described in other answers here.

2

First of all let me tell you that ~/.bashrc is the file which is executed every time a second shell is called up (when running a shell script, for instance), and ~/.profile is called on every login.

So I recommend you to write . ~/.bashrc command in your ~/.profile file, and this command will execute the bashrc file every time you login.

4
  • when .profile get executed on login it automatically execute the .bashrc file.
    – arun rana
    Sep 24, 2012 at 12:53
  • so .bashrc gets executed indirectly at every login
    – arun rana
    Sep 24, 2012 at 12:54
  • 1
    bashrc is for non-login shells. bash_profile/bash_login/profile/whatever else is for login shells. This is a source of a huge amount of contention, involving what is and isn't a login shell (a screen/tmux session for example, a terminal within a desktop environment, etc.). It's not necessarily the "second shell". It's a matter of shell entry. Jun 3, 2013 at 20:14
  • 4
    If you ever use a shell that isn't Bash, doing what you describe might cause you problems. .profile applies to all shells and .bashrc might contain language that some other shells don't understand. Instead of .profile, put what you wrote in .bash_profile. (Also, make the first thing .bash_profile does be "source .profile".)
    – Terry N
    Mar 19, 2014 at 16:44
1

I ran into the same issue after installing rbenv on my remote server (Digital Ocean) Ubuntu 16.04.

It somehow created ~/.bash_profile. It's the same ~/.bashrc on an OS X system.

And Ubuntu started to read only ~/.bash_profile, but not ~/.bashrc as it should.

So I just backed up my data in ~/.bash_profile with this:

mv ~/.bash_profile ~/.bash_profile.bak

And renamed the old ~/.bashrc to ~/.bash_profile

mv ~/.bashrc ~/.bash_profile

It worked for me. After my ssh login I have everything loaded in ~/.bashrc.

0

In case this might help anyone else...

Make sure you're editing the right .bashrc file for the right user. I had a similar issue using Vagrant. After running vagrant ssh my .bashrc file was not being run. The solution was simple in that I thought vagrant was logging in with the user named vagrant, and I was actually logging in as another user. So I was editing the wrong .bashrc file. After editing the correct .bashrc file my aliases were recognized.

-1

On Debian .bashrc is sourced from .profile this way:

if [ "$BASH" ]; then
  if [ -f ~/.bashrc ]; then
    . ~/.bashrc
  fi
fi

So I use this approach on other systems, where .bashrc is not sourced. You don't have to worry that the other shells may source Bash code, as the condition on the first line prevents it.

6
  • Why would another shell execute Bash specific startup scripts like? .bashrc is sourced only for a login shell where as .bash_profile is read for all interactive shells (i.e. Terminal) .profile is only read when .bash_profile doesn’t exist. macOS ≠ Linux so there’s no need for .bashrc and the script there isn’t for Debian, but for systems running XWindows/PTTY type logins. macOS does not do this.
    – Allan
    Mar 25 at 20:19
  • Yes, .profile is only read when .bash_profile doesn’t exist. And .bash_profile usually does not exist. And those who source .bashrc from .profile without if [ "$BASH" ] ... will source .bashrc while invoking the other shells that read .profile, which I think is undesirable.
    – Alek
    Mar 26 at 10:08
  • Since macOS doesn’t use those files by default, the only way they would show up is if someone put them in there incorrectly. Please note that you’re applying a Linux/Xorg convention to macOS and it is totally different.
    – Allan
    Mar 26 at 15:23
  • It's not "totally different", shells are the same programs whatever OS they're running on, that files might be put there deliberately and not "incorrectly". As for no reason to use .bashrc - that's not correct, it's used for non-login shells (BTW you confused it in the first comment, actually *profile files are for login shells)
    – Alek
    Mar 26 at 18:32
  • They”re different enough. Like I already said, Linux uses Xorg which require a login shell whereas macOS doesn’t. Since you can remote into macOS and get a login shell, .bash_profile covers everything, Using .bashrc and .profile only serve to add unneeded complexity.
    – Allan
    Mar 26 at 18:44
-2

To keep with the OSX style login .profile and support the normal bash behavior .bashrc you can use a .bashrc file if you switch between regular nix and OSX by providing a symbolic link to your .bashrc file called .bash_profile. Just make sure this file doesn't exist already before trying this, but this is how I do it.

ln -s .bashrc .bash_profile
1
  • By doing this linking between 2 differents shell initialisation script you are loosing a necessary separation of function. One day or another you will need an initialisation at the session level (.bash_profile) and an initialisation different at each shell level (.bashrc).
    – dan
    Sep 24, 2014 at 7:03
-2

/etc/bashrc is for global profiles on mac systems. You can add: alias ls="ls -G" to the top of this file and your good, everyone will have colorized files and folders on the system so you will not have to set anything extra for each individual user. You will also notice along with "sudo su -" to root from yourself you will still have colorized files and folders with no extra headaches.

Another fun tip: When using mac's Iterm2, importing color profiles will render the coloring of the files and folders immediately according to your color scheme after having this "/etc/bashrc" option set.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .