111

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?

8 Answers 8

147

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

6
  • 17
    This 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
    Feb 13, 2011 at 18:49
  • 1
    Or one can use bash_aliases which has the same effect as putting the aliases in bashrc, but more manageable: ss64.com/osx/syntax-bashrc.html
    – Atul Ingle
    Dec 10, 2013 at 13:22
  • 6
    in my .bash_profile I just wrote one line to alias (sort of ) bashrc -> source ~/.bashrc Apr 5, 2016 at 16:49
  • a girl almost threw the mac at me for arguing blindly that placing anything in ~/.bashrc on will work. Oct 14, 2020 at 7:19
  • @EricHodonsky is living in 2030. You're a lion amongst sheep.
    – Sam
    Feb 25, 2021 at 6:05
114

.[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.

7
  • 18
    +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. Feb 12, 2011 at 18:51
  • 2
    True. Since exported instance variables are inherited, I just set them in .profile instead of .bashrc.
    – LaC
    Feb 13, 2011 at 11:28
  • 1
    @LaC can you explain _Since exported instance variables are inherited, I just set them in .profile_…?
    – sam
    Jan 14, 2014 at 18:11
  • 1
    @sam, I don't know where "instance" came from. I just meant "exported variables". Unfortunately I cannot edit that comment.
    – LaC
    Jan 15, 2014 at 6:35
  • 3
    @dinosaur: "-r" checks if the file is readable.
    – mhvelplund
    Jun 4, 2016 at 8:48
19

Since MacOS Catalina zsh is the default shell. On this OS add the alias into ~/.zshrc

Pre Catalina add the alias to ~/.bash_profile

3
  • 2
    This should be the top answer.
    – o12d10
    Aug 8, 2021 at 18:14
  • This worked for me on Big Sur. Terminal is using zsh. Aug 12, 2021 at 15:55
  • 1
    This worked for me on Ventura. Nov 27, 2022 at 22:18
9

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

ln -s .bashrc .bash_profile
1
  • 5
    On newer versions of OS X, the terminal may be using ZSH, which means .zshrc should be used instead of .bash_profile. Aug 12, 2021 at 15:58
9

I tried using the solution to update .bash_profile and .bashrc but that did not work because MacOS >= 10.15 (Catalina) is using zsh as default.

So:

  • create a new file, ~/.zprofile, add aliases there.
  • use command source ~/.zprofile to execute in same shell or just open a new terminal.

and your changes should be permament.

1
  • command 'source ~/.zprofile' just executes your code in bashrc or zprofile. Aug 10, 2023 at 15:03
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

2

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.

2
  • This is little more than a rehash of the answers from four years ago. May 5, 2015 at 1:36
  • 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
    May 5, 2015 at 19:23
0

If you are on MacOS, you might be using the Z shell. To check, type:

echo $0

See if the output is -zsh. If so, you need to add your lines into ~/.zshrc

1
  • The question has already been answered and accepted and it was not zsh. Aug 25, 2023 at 14:25

You must log in to answer this question.

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