From the bash man page:
When bash is invoked as an interactive login shell, or as a non-interactive shell with the --login option, it first reads and executes commands 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.
When an interactive shell that is not a login shell is started, bash reads and executes commands from ~/.bashrc, if that file exists. This may be inhibited by using the --norc option. The --rcfile file option will force bash to read and execute commands from file instead of ~/.bashrc.
macOS is unusual in that it runs each terminal session as a login shell, most other versions of unix, linux and many GUI terminal emulators do not run as login shells.
Commands you will typically want to include in .bashrc include: history variables to increase the history available and bind keymappings.
If you want to put startup PATH and common settings in ~/.bashrc either because you have a GUI terminal, or just to be consistent with other OS's, then source (load) .bashrc from .bash_profile by adding the following lines to .bash_profile:
if [ -f ~/.bashrc ]; then
source ~/.bashrc
fi
Now when you login to your machine from a console .bashrc will be called.
The alternative is simply to put everything into .bash_profile if you are only running the macOS terminal then every new window/tab that you open will load .bash_profile
On a brand new user account, none of these files will exist, they can be created with any suitable text editor (BBedit/Text Wrangler) that is capable of creating plain text files with unix style (LF) line endings. Save them into your home folder (~/)
If you have a lot of shell configurations, you may want to split them out into several subfiles and use the source builtin to load them from .bashrc:
source ~/.bash-options
source ~/.bash-aliases
source ~/.bash-functionsAlternatively, to ensure the files actually exist before loading
if [ -f ~/.bash-aliases ]; then
. ~/.bash-aliases
fiThe command . ~/.bash-aliases will source ~/.bash-aliases in the context of the currently running shell.
This is particularly useful for adding aliases, the separate file makes it easier to re-load them when you make changes.
Related macOS comands:
Hanye -
example .bash_aliases
bashrc_dispatch - use symlinks to reorganise .bashrc, .bash_profile and .profile
alias - Create an alias
macOS Syntax