MacOS – How to persistently define aliases in Terminal

aliasmacosscriptterminal

I want to add aliases to my environment so that they are available in all Terminal instances.

I got this answer:

Yes, you can put it in your .bashrc, .bash_profile or .profile. On some systems the shell init scripts also source .bash_aliases or .aliases so if one of those files exists on your system it may make sense to use them.`

But I cannot find any of these files.

(I configured OS X to see hidden files, and yet I don't see any of the above 4.)

Best Answer

If you use bash, add the alias commands to ~/.bashrc and save a file like this as ~/.bash_profile:

. ~/.bashrc

When bash is invoked as an interactive non-login shell, it reads .bashrc but not .bash_profile. When bash is it is invoked as an interactive login shell, it reads .bash_profile but not .bashrc.

Terminal and iTerm open new shells as login shells by default, so many OS X users just use .bash_profile as their personal configuration file. For example tmux and the shell mode in Emacs open new shells as non-login shells though.

.profile is also read by other shells like ksh. If both .bash_profile and .profile exist, bash only reads .bash_profile when it is invoked as an interactive login shell.

See https://www.gnu.org/software/bash/manual/html_node/Bash-Startup-Files.html for more information.