I am trying to create a permanent alias for my terminal. I put the alias in my ~/.profile, ~/.bashrc, and ~/.bash_profile files, previously empty. When I start a new terminal, bash does not recognize the alias, but if I source any of them, it does. Why are these not getting run when I open a terminal? I am on OSX.
-
2what terminal are you using? iTerm? – ptierno Jul 29 '14 at 22:19
-
Useful to understand why: [Why doesn't .bashrc run automatically?](https://apple.stackexchange.com/a/13019/116146) and [What is the difference between .bash_profile and .bashrc?](https://apple.stackexchange.com/questions/51036/what-is-the-difference-between-bash-profile-and-bashrc) – Ricardo Mar 19 '21 at 00:55
11 Answers
Newer MacOS versions use zsh as the default shell for both Terminal and iTerm2. Run echo $SHELL
to confirm if this is the case for you.
Zsh looks for a .zshrc
file upon shell startup, so to continue using zsh while sourcing the contents of your bash profile, you can run the following:
echo "source ~/.bash_profile" >> ~/.zshrc
Open a new terminal window for the changes to take effect.

- 579
- 4
- 3
-
How do I undo this? That is working for me and now my terminal text is all messed up. – thenomadicmann Feb 08 '21 at 01:40
-
1This is deeply flawed. You should never have one shell shource the startup files of another. – tripleee Jul 09 '21 at 10:51
-
1@thenomadicmann You should edit the `.zshrc` file and remove the `source` statement you added as the very last line. – tripleee Jul 09 '21 at 10:51
-
@tripleee to make your feedback more constructive, I suggest identifying some of the risks with this approach and suggesting an alternative – lchapo Jul 10 '21 at 15:49
-
@lchapo Several answers here identify other approaches. I don't think explaining the risks in more detail is really necessary here, and I'm repeating things I have said in other comrents here; but trivially, using Bash-only syntax in this file will break it for any shell which doesn't support that syntax. (Zsh can be configured to understand Bash syntax, but this locks you into requiring this configuration option, which then raises doubts about the rationale of preferring Zsh over Bash.) – tripleee Jul 10 '21 at 15:56
Two things need to happen here when using iTerm
to get the loading of dotfiles to work.
First you should add the following to your .bash_profile
[[ -s ~/.bashrc ]] && source ~/.bashrc
Secondly you will need to ensure that in iTerm
preferences your terminal is set to launch a login shell.
Hope this helps!

- 9,534
- 2
- 23
- 35
Using the default mac terminal, what worked for me was to add a command to run on start up to source my .bash_profile.
Preferences > Profile > Startup > Add command 'source ~/.bash_profile'
Mac terminal preferences window screenshot
Might be considered to be a bit hacky, but it does the trick.

- 351
- 3
- 6
As of Catalina the default shell is now zsh. You can change it back to bash with chsh -s /bin/bash
and that should load your .profile
or .bash_profile

- 14,906
- 5
- 42
- 37
-
See this post for details for setting up nvm on zsh: https://stackoverflow.com/a/47017363/230119 – Jeremy Jan 25 '20 at 00:20
Why are your shell's initialization files not loading?
As with most things, It Depends ™
I recently experienced the same phenomenon and went through the following exercise to resolve it:
I use iTerm. iTerm runs a login shell by default. Verify in iTerm Preferences > General > Command > (*) Login Shell Therefore, I know that ~/.bash_profile will always be called.
Knowing that, I put the following in my ~/.bash_profile file:
for file in ~/.{bashrc,bash_exports,bash_aliases,bash_functions}; do
[ -r "$file" ] && source "$file"
done
unset file
Notice that I use separate files for .bashrc, .bash_exports, etc. It keeps things separate and simple.
Note also that /etc/profile is loaded first, but since I have never used that system wide init file, I knew that that was not my problem. For details check out $ man bash
So, I started with my ~/.bash_profile file.
I found that when I installed Canopy Express that it's installer replaced the contents of my ~/.bash_profile file with the following content:
# Added by Canopy installer on 2017-04-19
# VIRTUAL_ENV_DISABLE_PROMPT can be set to '' to make the bash prompt show that Canopy is active, otherwise 1
alias activate_canopy="source '/Users/lex/dev/python/User/bin/activate'"
# VIRTUAL_ENV_DISABLE_PROMPT=1 source '/Users/lex/dev/python/User/bin/activate'
p.s. Canopy is an excellent, free python IDE, that I highly recommend.
Fortunately, I backup my ~/.bash* files so restoring that was easy and quickly fixed my issue.
My advice would be to understand the order of calls to your initialization files and start with the first one and work your way through them until you find the problem.
Oh, and you may want to verify which shell you are using (I use bash):
~ $ echo $SHELL
/usr/local/bin/bash

- 30,760
- 1
- 55
- 36
As of High Sierra, both Terminal and iTerm want to load ~/.profile first. So I suggest you put one line in your .profile to make your Mac work like other Unixes:
source ~/.bash_profile
By editing this one file, you won't have to search through the menus of multiple apps to override Apple's bizarre behavior in each.

- 219
- 1
- 3
-
-
This is incorrect, because it will cause non-Bash shells to attempt to source a file which is reserved for Bash and may contain syntax which is not compatible with other shells. (Doing the reverse is harmless; edit `.bash_profile` to `source ~/.profile` which is designed for this.) – tripleee Jul 09 '21 at 10:53
Little late to the party but it seems that the file .zprofile is the equivalent to that of .bash_profile when loading zsh. I used this instead to execute a few commands on startup. Of course this only valid for a specific iTerm setup with zsh.

- 30,962
- 25
- 85
- 135

- 11
- 1
-
Thank you, copying .bash_profile into .zprofile and restarting the terminal did the trick for me – Nipazz Jul 20 '23 at 11:41
I am guessing you may use another shell, such as bash, tcsh, sh, zsh etc.
Put source .bash_profile
into your appropriate 'bashrc' file will make the auto loading recovered, i.e.
.login for tcsh, .bash_profile for bash, .zshrc for zsh

- 700
- 1
- 8
- 22
Most likely, you need to create the files yourself as they appear not to exist by default. You should give them execute permission to make them run.
~ % sudo chmod 700 ~/.bash_profile
Also, you should check the ownership of the files. They should belong to current user rather than root. Otherwise, you will get permission denied
error.
~ % ls -a -l
~ % sudo chown <user_name> ~/.bash_profile
Finally, please note that bash looks in your home directory for .bash_profile, .bash_login, and .profile in order. Bash will stop looking if the first is found. This means if you have both .bash_profile and .profile files, the .profile will not run. For more information
Hope this would help you.

- 135
- 2
- 11
-
None of this nonsense will be necessary if you didn't incorrectly use `sudo` to manipulate your personal files in the first place. This is a common beginner error as such, but still not a good general solution for other situations. – tripleee Jul 09 '21 at 10:56
-
Again, the only possible situation where `sudo` will be necessary and useful is if you incorrectly used `sudo` before. You should never use `sudo` to manipulate your own files because that will cause them to be owned by `root` instead of yourself, which is basically a system integrity error. – tripleee Jul 10 '21 at 17:17
My issue was solved by unchecking Preferences > General > tmux >
Use "tmux" profile rather than profile of the connecting session

- 136
- 1
- 3