How do I create a Bash alias?
I'm on OSX and I need to put something like this, alias blah="/usr/bin/blah"
in a config file but I don't know where the config file is.
You can add an alias
or a function
in your startup script file. Usually this is .bashrc
, .bash_login
or .profile
file in your home directory.
Since these files are hidden you will have to do an ls -a
to list them. If you don't have one you can create one.
If I remember correctly, when I had bought my Mac, the .bash_login
file wasn't there. I had to create it for myself so that I could put prompt info
, alias
, functions
, etc. in it.
Here are the steps if you would like to create one:
- Start up Terminal
- Type
cd ~/
to go to your home folder - Type
touch .bash_profile
to create your new file. - Edit
.bash_profile
with your favorite editor (or you can just typeopen -e .bash_profile
to open it in TextEdit. - Type
. .bash_profile
to reload.bash_profile
and update any alias you add.
I just open zshrc with sublime, and edit it.
subl .zshrc
And add this on sublime:
alias blah="/usr/bin/blah"
Run this command in terminal:
source ~/.zshrc
Done.
On OS X you want to use ~/.bash_profile. This is because by default Terminal.app opens a login shell for each new window.
See more about the different configuration files and when they are used here: What's the difference between .bashrc, .bash_profile, and .environment?
and in relation to OSX here: About .bash_profile, .bashrc, and where should alias be written in?
MacOS Catalina and Above
Apple switched their default shell to zsh, so the config files include ~/.zshenv
and ~/.zshrc
. This is just like ~/.bashrc
, but for zsh. Just edit the file and add what you need; it should be sourced every time you open a new terminal window:
nano ~/.zshenv
alias py=python
Then do ctrl+x, y, then enter to save.
This file seems to be executed no matter what (login, non-login, or script), so seems better than the ~/.zshrc
file.
High Sierra and earlier
The default shell is bash, and you can edit the file ~/.bash_profile
and add aliases:
nano ~/.bash_profile
alias py=python
Then ctrl+x, y, and enter to save. See this post for more on these configs. It's a little better to set it up with your alias in ~/.bashrc
, then source ~/.bashrc
from ~/.bash_profile
. In ~/.bash_profile
it would then look like:
source ~/.bashrc