82

I have read so many suggestions about, not putting your customization aka commands in ".profile" file. Rather, create a .bash_profile for yourself and add your alias and etc.

But,when I open the new terminal, if there is only .bash_profile, OS X is not exporting/sourcing the commands mentioned in it. I have to manually source the .bash_profile.

If I create .profile file, on opening a new terminal, all my commands in .profile are executed and will be available readily.

Could you please help me in understanding, how does it works? Also, when to use .bashrc/.profile/.bash_profile files.

Thanks!

cherryhitech
  • 2,089
  • 3
  • 17
  • 15

8 Answers8

107

According to Apple,

zsh (Z shell) is the default shell for all newly created user accounts, starting with macOS Catalina.

So you should verify your default shell with the command:

$ echo $SHELL

If the result is /bin/bash your default shell is BASH, and if the result is /bin/zsh the default is ZSH.

Go to home with $ cd ~/ and create the profile (if it does not exist) and edit it with the commands:

For bash:

$ touch .bash_profile
$ open .bash_profile

For ZSH:

$ touch .zprofile
$ open .zprofile
Fabian
  • 3,310
  • 3
  • 26
  • 35
Doug
  • 1,643
  • 2
  • 13
  • 10
  • 6
    You are a hero. I've been trying to figure out why .bash_profile/.bashrc/.profile don't work for half an hour. – Cody Wikman Oct 29 '20 at 21:27
  • Thanks!! Z shell threw me for a loop and googling turns up all of the older methods... – songololo Nov 19 '20 at 17:57
  • thanks man! I used a mac after 4 years and you saved me. i was stuck with bash_profile – sud007 Jul 17 '21 at 12:07
  • For Apple M1 chip, this one worked. Look no further, thank you Doug.! – Dhamo Jul 31 '21 at 08:37
  • Thank you @Doug for your answer. – Alapan Das Aug 17 '21 at 16:39
  • Thanks, this is crazy I guess my original mac account was created pre-catalina and was using the typical .bash_profile. I recently ran into login issues, and my user account was recreated on Big Sur which I currently run. I was able to fix my env variables issues by "cd ~/" then "cp .bash_profile .zprofile" – Danuofr Sep 16 '22 at 00:33
92

According to the manual page that ships with OS X:

... 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. The --noprofile option may be used when the shell is started to inhibit this behavior.

It should only read ~/.profile as a last resort if neither ~/.bash_profile nor ~/.bash_login are readable.

On all of my OS X systems, I have my ~/.bash_profile set to:

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

It is highly recommended that you do this on OS X in order to get bash to read your ~/.bashrc file like you would expect.

Andon M. Coleman
  • 42,359
  • 2
  • 81
  • 106
  • 1
    I had the same problem with command aliases in `~/.profile` not being put into effect in Terminal, and fixed the problem by using your method of moving the commands into `~/.bashrc` (and deleting `~/.profile` and making the little `~/.bash_profile`). However, now when I execute `echo $PATH` all my path entries appear twice in the output. So something's still wrong! – murray Feb 09 '14 at 17:20
  • weird. had `.bash_profile` and terminal.app is ignoring it here. moved the file (kept all the same permissions) to `.profile` and now it is happy. – gcb Nov 27 '15 at 03:35
  • 59
    Reminder if you are using `zsh` instead of `bash`. The terminal will source `.zshrc` instead of `.bash_profile`. – AlbertSamuel Dec 05 '16 at 23:28
  • 6
    As of today, the default terminal shell in OSX Cataline is `zsh`, so @AlbertSamuel's comment is should be turned into an answer. – Our May 01 '20 at 08:43
  • @AlbertSamuel Thanks! Just got a new mac and this solved it – Cade Embery Jun 19 '20 at 22:58
59

It's also possible that your terminal shell is defaulting to sh instead of bash. You can verify this first:

$ echo $SHELL
/bin/tcsh

To change this to bash, you can go into your Terminal -> Preferences -> Startup tab, and change "Shell Opens With:" from "Default login shell" to Command and value "/bin/bash".

Alternately, you can change your default shell by executing the following command at the command prompt:

chsh -s bin/bash

After you do one of these, open a new shell window, and your .bash_profile should be sourced.

Matt S
  • 1,892
  • 16
  • 15
  • 7
    I found that I wanted to use sh instead of bash. I use zsh and I found this comment to be helpful: https://github.com/robbyrussell/oh-my-zsh/issues/3807#issuecomment-187930190 - basically add `source ~/.bash_profile` to the bottom of your `~/.zshrc` config – thedanotto Dec 13 '18 at 17:56
  • Yes, that work! Just, add `source ~/.bash_profile` to the bottom of your `~/.zshrc`. – Fran Bonafina Sep 30 '21 at 23:17
  • Add source ~/.bash_profile to the bottom of your ~/.zshrc. This should be an answer – Lexsoul Jul 14 '22 at 18:20
20

For anyone else who finds this, instead of bash_profile, for new versions of mac you can use .zshrc. I.E., do

open .zshrc

and add what you need there.

Sabrina Leggett
  • 9,079
  • 7
  • 47
  • 50
7

You can use zsh to fix the problem.

The Z shell (also known as zsh) is a Unix shell that is built on top of bash (the default shell for macOS) with additional features. It's recommended to use zsh over bash.

Installation

  1. Install zsh using Homebrew: $ brew install zsh
  2. Install Oh My Zsh: $ sh -c "$(curl -fsSL https://raw.githubusercontent.com/robbyrussell/oh-my-zsh/master/tools/install.sh)"
  3. Move to .bash_profile setting .zshrc file
  4. To apply the changes you make you need to either start new shell instance or run: source ~/.zshrc
hong developer
  • 13,291
  • 4
  • 38
  • 68
5

If you are using zsh, you can source to .bash_profile by adding the following line to .zprofile

if [ -f ~/.bash_profile ]; then
    source ~/.bash_profile
fi
Brij Vaid
  • 53
  • 1
  • 2
3

It should be mentioned that bash will first look for a /etc/profile file, as stated in the Bash man pages.

When bash is invoked as an interactive login shell, or as a non-inter- active shell with the --login option, it first reads and executes com- mands 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. The --noprofile option may be used when the shell is started to inhibit this behavior.

Steve Benner
  • 1,679
  • 22
  • 26
  • 3
    That `bash` on OSX by default doesn't source `~/.bashrc` is unrelated to any content in `/etc/profile`. Rather, it has to do with the fact that `Terminal.app` creates _every_ bash shell as a _login_ session, so that only `~/.bash_profile` or `~/.bash_login` or `~/.profile` (whichever is found fist) are sourced. If you want `~/.bashrc` to be sourced too, source it explicitly from `~/.bash_profile`, as in @Andon M. Coleman's answer. (If you explicitly start a bash session as an _interactive NON-login session_, `~/.bashrc` gets sourced automatically.) – mklement0 Apr 23 '14 at 03:35
  • You are absolutely right, and I posted my explanation too hastily. Thank your for the correction; I edited my answer and upvoted your comment as it explains correctly what I had attempted to describe. – Steve Benner Apr 24 '14 at 01:38
  • 1
    On OSX El Capitan, I noticed that root runs under /bin/sh (I believe that's the Ash shell?) instead of the Bash shell. But when I open a terminal prompt and check the $SHELL var, it reads /bin/bash. So, what script do we edit for the case of /bin/sh? – Volomike May 30 '16 at 04:11
-4

I solved by simply adding bash (in a newline) into ~/.bash_profile file.

Anshul
  • 117
  • 1
  • 2