113

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?

Daniel Beck
  • 111,893

8 Answers8

150

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
  • 4,815
  • 2
  • 28
  • 23
118

.[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
  • 3,019
20

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

Pre Catalina add the alias to ~/.bash_profile

10

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

ln -s .bashrc .bash_profile
Barrett
  • 101
  • 1
  • 2
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.

Destroy666
  • 12,350
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
  • 19,619
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.

Ricardo
  • 195
1

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