Mac OS X .bashrc not working

83

33

I have a one-line .bashrc file in my home directory:

alias countlines='find . -type f -print0 | xargs -0 cat | wc -l'

But it is not creating the alias. Why might that be?

William Jockusch

Posted 2011-02-12T13:58:17.023

Reputation: 2 913

Answers

108

In OSX, .bash_profile is used instead of .bashrc.

And yes, the .bash_profile file should be located in /Users/YourName/
(In other words, ~/.bash_profile)

For example, /Users/Aaron/.bash_profile

Azz

Posted 2011-02-12T13:58:17.023

Reputation: 3 777

5in my .bash_profile I just wrote one line to alias (sort of ) bashrc -> source ~/.bashrc – Eric Hodonsky – 2016-04-05T16:49:36.853

14This is not the right answer. Aliases are not inherited, so, if you only define them in .bash_profile, they won't be defined in non-login shells (eg when you run bash inside bash). – LaC – 2011-02-13T18:49:16.580

1

Or one can use bash_aliases which has the same effect as putting the aliases in bashrc, but more manageable: http://ss64.com/osx/syntax-bashrc.html

– Atul Ingle – 2013-12-10T13:22:21.293

95

.[bash_]profile and .bashrc can be used on both OS X and Linux. The former is loaded when the shell is a login shell; the latter when it is not. The real difference is that Linux runs a login shell when the user logs into a graphical session, and then, when you open a terminal application, those shells are non-login shells; whereas OS X does not run a shell upon graphical login, and when you run a shell from Terminal.app, that is a login shell.

If you want your aliases to work in both login and non-login shells (and you usually do), you should put them in .bashrc and source .bashrc in your .bash_profile, with a line like this:

[ -r ~/.bashrc ] && source ~/.bashrc

This applies to any system using bash.

LaC

Posted 2011-02-12T13:58:17.023

Reputation: 2 263

I know what source ~/.bashrc does; but what does [ -r ~/.bashrc ] do, and is it necessary? – dinosaur – 2016-06-04T05:22:39.467

2@dinosaur: "-r" checks if the file is readable. – mhvelplund – 2016-06-04T08:48:30.777

...pertinently for the current situation, exists and is readable, so if there is no ~/.bashrc, that check prevents an error being caused by trying to source a nonexisting file. – Charles Duffy – 2017-03-06T15:37:35.483

16

+1 with the caveat that everything in .bashrc will be run again for sub-shells (and subsub-, subsubsub-, etc), so e.g. PATH=$PATH:/my/private/binaries will lead to PATH bloat. See this for a workaround.

– Gordon Davisson – 2011-02-12T18:51:55.497

2True. Since exported instance variables are inherited, I just set them in .profile instead of .bashrc. – LaC – 2011-02-13T11:28:58.490

1@LaC can you explain Since exported instance variables are inherited, I just set them in `.profile`…? – sam – 2014-01-14T18:11:52.520

1@sam, I don't know where "instance" came from. I just meant "exported variables". Unfortunately I cannot edit that comment. – LaC – 2014-01-15T06:35:39.400

7

Or create a sym link called .bash_profile pointed at your .bashrc

ln -s .bashrc .bash_profile

Barrett

Posted 2011-02-12T13:58:17.023

Reputation: 71

3

It is not being aliased because .bash_profile is used instead of .bashrc on Mac OS X.

So you have two options:

  • Put the alias in your ~/.bash_profile

  • Or source your .bashrc from your .bash_profile by adding this line to the .bash_profile:

    . ~/.bashrc

Wuffers

Posted 2011-02-12T13:58:17.023

Reputation: 16 645

1

On Mac OS X Yosemite, run the following command:

vi ~/.profile

Then add the following line:

source ~/.bashrc

Now save and close .profile, then open a new Terminal window or just run:

source ~/.profile

See also this answer. It worked on v10.10.3.

Ricardo

Posted 2011-02-12T13:58:17.023

Reputation: 133

This is little more than a rehash of the answers from four years ago. – G-Man Says 'Reinstate Monica' – 2015-05-05T01:36:09.953

Sure, just an easy eay to read and apply it. Plus a small contribution - since the other mentioned files were not available on my OS Yosemite. – Ricardo – 2015-05-05T19:23:31.060