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.

- 39,467
- 16
- 112
- 140

- 48,619
- 62
- 153
- 231
-
Tangentially, this particular alias is basically redundant unless you are specifically trying to override he shell's `PATH` resolution for this specific command. If `/usr/bin` is in your `PATH` (which really it must be) then `blah` will run from there just fine without this alias, too, unless there is also say `/usr/local/bin/blah` and `/usr/local/bin` is before `/usr/bin` in your `PATH` *but* you still want to prefer `/usr/bin/blah` and cannot for some reason simply remove or rename `/usr/local/bin/blah`. – tripleee Jan 05 '18 at 05:49
-
Even then this is a dubious, hackish solution which should come with a big fat ugly comment with a warning message telling you how this will bite your shapely lower abdomen, and another saying "I told you so" for each time this happened. – tripleee Jan 05 '18 at 05:55
-
1For all the automation lovers: I personally add `addAlias() { echo "alias $1='$2' " >> ~/.bash_profile` to my bash_profile and then I source it and run `addAlias hi 'echo "hi" '` . Source bash profile and type `hi` to see it. – devssh Jan 23 '18 at 07:35
-
Make sure to escape characters properly alias name=" echo "string" " is wrong, but alias name=" echo \"string\" " will be fine. – Michael Hearn Nov 28 '22 at 21:36
16 Answers
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.

- 158,662
- 42
- 215
- 303

- 74,723
- 23
- 102
- 147
-
11Awesome, thanks @jaypal! Is the command `source .bash_profile` an alternative to step 5? – Ian Campbell Sep 03 '13 at 03:10
-
10Yes on my OSX Mavericks there was no `.bash_profile` in my home dir. Creating it, adding the alias to it, and then initiating it with the `. .bash_profile` command worked. – Bradley Flood Jun 26 '14 at 01:27
-
1
-
6On OSX El Capitan `.bash_profile` works. If it doesn’t automatically load when you open a terminal window, it is probably because it was created without executable permission. This command will fix it and it should automatically load for future sessions: `chmod u+x .bash_profile` – Mischinab Jul 05 '16 at 13:09
-
1
-
-
To get aliases working on High Sierra, I had to add `source ~/.bashrc` to `.bash_profile`, as instructed here https://stackoverflow.com/a/902955/4039886 – klaevv Jul 10 '18 at 07:27
-
Just a little comment here. You need to close entirely your terminal and reopen to make it work properly – Jean Manzo Sep 12 '20 at 14:25
-
A little bit of additional information on the above, If you want to add your aliases in the .bashrc file, make sure you add "source .bashrc" in the .bash_profile file and reload it after adding. – Mythili Oct 05 '21 at 05:06
-
Just in case it's still not working, restarting the tab alone won't work, we have to restart the terminal - For Dumbos like me :) – Panduranga Rao Sadhu Dec 20 '21 at 06:18
-
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.
-
14This is confused. If your shell is Bash, many other answers on this page are more detailed and helpful. If you use Zsh instead of Bash, then obviously you should `source .zshrc` at the end to load in the changes you made into your currently running shell instance. – tripleee Jan 05 '18 at 05:41
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

- 13,746
- 5
- 87
- 117
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?

- 1
- 1

- 696
- 5
- 7
In my .bashrc
file the following lines were there by default:
# Alias definitions.
# You may want to put all your additions into a separate file like
# ~/.bash_aliases, instead of adding them here directly.
# See /usr/share/doc/bash-doc/examples in the bash-doc package.
if [ -f ~/.bash_aliases ]; then
. ~/.bash_aliases
fi
Hence, in my platform .bash_aliases
is the file used for aliases by default (and the one I use). I'm not an OS X user, but I guess that if you open your .bashrc
file, you'll be able to identify what's the file commonly used for aliases in your platform.

- 39,419
- 8
- 102
- 133
-
If the profile doesn’t automatically load when you open a terminal window, it is probably missing the executable permission. This command will fix it and it should automatically load for future sessions: `chmod u+x .bash_profile` – Mischinab Jul 05 '16 at 13:13
-
1@Mischinab No, the file just needs to be readable in order for Bash to read it as its configuration file. Making it executable is technically harmless but unnecessary. I would discourage it on nontechnical grounds (you might end up confusing yourself and/or others). – tripleee Jan 05 '18 at 05:45
It works for me on macOS Mojave
You can do a few simple steps:
open terminal
sudo nano /.bash_profile
add your aliases, as example:
some aliases
alias ll='ls -alF' alias la='ls -A' alias eb="sudo nano ~/.bash_profile && source ~/.bash_profile" #docker aliases alias d='docker' alias dc='docker-compose' alias dnax="docker rm $(docker ps -aq)" #git aliases alias g='git' alias new="git checkout -b" alias last="git log -2" alias gg='git status' alias lg="git log --pretty=format:'%h was %an, %ar, message: %s' --graph" alias nah="git reset --hard && git clean -df" alias squash="git rebase -i HEAD~2"
source /.bash_profile
Done. Use and enjoy!

- 377
- 1
- 5
- 11
-
My aliases do not work with double dashes -- in case of git. How did you get them to work? For example your `alias lg` has double dashes. I'm trying to set `alias stash="git stash --include-untracked"` but when I call it in terminal it ignores the -- command. – Ali Kazi Jul 31 '20 at 01:34
-
-
1
-
Just in case it's still not working, restarting the tab alone won't work, we have to restart the terminal - For Dumbos like me :) – Panduranga Rao Sadhu Dec 20 '21 at 06:17
For macOS Catalina Users:
Step 1: create or update .zshrc file
vi ~/.zshrc
Step 2: Add your alias line
alias blah="/usr/bin/blah"
Step 3: Source .zshrc
source ~/.zshrc
Step 4: Check you're alias, by typing alias on the command prompt
alias

- 797
- 7
- 17
cd /etc
sudo vi bashrc
Add the following like:
alias ll="ls -lrt"
Finally restart Terminal.

- 4,129
- 5
- 32
- 49

- 119
- 1
- 2
-
1After adding alias. Run 'source' on your '.bash_profile' file. Ex: source ~/.bash_profile (command which activates/ reloads the bash aliases) – UIResponder Jul 10 '17 at 13:01
The config file for scripts and programs is ~/.bashrc
and the config file that gets loaded when you use Terminal is ~/.bash_login
.
I think the best way is to just have everything in ~/.bashrc
.
For your specific question just enter (this will overwrite any existing ~/.bashrc):
echo "alias blah=\"/usr/bin/blah\"" >>~/.bashrc
into the Terminal and a ~/.bashrc
file will be created with your new alises. After that just edit the file to add new aliases, functions, settings etc.

- 4,962
- 3
- 32
- 34
-
5Shouldn't you use `>>` instead of `>` so that it appends to the file rather than replacing everything in it? – Jonny Apr 09 '17 at 21:41
- Go to home
- Open .bashrc
Create alias at bottom of the file
alias alias_name='command to do' eg: alias cdDesktop='cd /Desktop'
Save the file
source .bashrc
source ~/.bashrc
Open terminal (Ctrl+Alt+T) & type cdDesktop & press enter

- 2,708
- 2
- 24
- 34
If you put blah="/usr/bin/blah"
in your ~/.bashrc
then you can use $blah
in your login shell as a substitute for typing /usr/bin/blah

- 135,741
- 24
- 144
- 154
-
2The dollar sign is emphatically not in any way necessary, useful, or correct here. – tripleee Jan 05 '18 at 05:48
I need to run the Postgres database and created an alias for the purpose. The work through is provided below:
$ nano ~/.bash_profile
# in the bash_profile, insert the following texts:
alias pgst="pg_ctl -D /usr/local/var/postgres start"
alias pgsp="pg_ctl -D /usr/local/var/postgres stop"
$ source ~/.bash_profile
### This will start the Postgres server
$ pgst
### This will stop the Postgres server
$ pgsp

- 11,321
- 18
- 114
- 168
create a bash_profile at your user root - ex
/user/username/.bash_profile
open file
vim ~/.bash_profile
add alias as ex. (save and exit)
alias mydir="cd ~/Documents/dirname/anotherdir"
in new terminal just type mydir - it should open
/user/username/Documents/dirname/anotherdir

- 4,315
- 1
- 28
- 19
To create a permanent alias shortcut, put it in .bash_profile file and point .bashrc file to .bash_profile file. Follow these steps (I am creating an alias command called bnode to run babel transpiler on ES6 code):
- Go to terminal command prompt and type “cd” (this will take you to the home directory. Note: even though your programming files may be located on your “D: drive”, your “.bash” files may be located on your “C: drive” )
- To see the location of the home directory, type “pwd” (this will show you the home directory path and where the .bash files are probably located)
- To see all dot "." files in the home directory, type “ls -la” (this will show ALL files including hidden dot "." files)
- You will see 2 files: “.bash_profile” and “.bashrc”
- Open .bashrc file in VS Code Editor or your IDE and enter “source ~/.bash_profile” in first line (to point .bashrc file to .bash_profile)
- Open .bash_profile file in VS Code Editor and enter “alias bnode='./node_modules/.bin/babel-node'” (to create permanent bnode shortcut to execute as bash command)
- Save and close both files
- Now open the file you want to execute (index.js) and open in terminal command prompt and run file by using command “bnode index.js”
- Now your index.js file will execute but before creating bnode alias in .bash_profile file you would get the error "bash: bnode command not found" and it would not recognize and give errors on some ES6 code.
- Helpful link to learn about dotfiles: https://dotfiles.github.io/
I hope this helps! Good luck!

- 5,392
- 3
- 19
- 45

- 5,503
- 2
- 16
- 9
I think it's proper way:
1) Go to teminal. open ~/.bashrc
. Add if not exists
if [ -f ~/.bash_aliases ]; then
. ~/.bash_aliases
fi
2) open ~/.bash_aliases
. If not exists: touch ~/.bash_aliases && open ~/.bash_aliases
3) To add new alias rather
- edit .bash_aliases
file and restart terminal or print source ~/.bash_aliases
- print echo "alias clr='clear'" >> ~/.bash_aliases && source ~/.bash_aliases
where your alias is alias clr='clear'
.
4) Add line source ~/.bash_aliases
to ~/.bash_profile
file. It needs to load aliases in each init of terminal.

- 12,630
- 8
- 75
- 122