Make an Alias in Bash or Zsh Shell in macOS with Terminal

To make aliases of macOS Unix commands in your bash or zsh shell on macOS and earlier versions, it is done via your .bash_profile or .zshrc file which lives in your home account directory, if the file does not already exist, just create one.

As of macOS 10.6 Catalina and its successor Big Sur, Apple has made the zsh shell the default shell, previously it was the bash shell.

Launch Terminal from the /Application/Utilities folder

Go to your home directory by just entering ‘cd’ followed by the ‘return’ key to enter the command:

cd

List your home directory contents including invisible files to see if the file already exists, use:

ls -la
drwxr-xr-x+ 18 ladmin staff 612 Jul 14 09:21 .
drwxr-xr-x 6 root admin 204 Jul 3 18:28 ..
-rw------- 1 ladmin staff 3 Jun 2 12:53 .CFUserTextEncoding
-rw-r--r--@ 1 ladmin staff 6148 Jul 14 09:01 .DS_Store
drwx------ 5 ladmin staff 170 Jul 3 18:44 .Trash
-rw------- 1 ladmin staff 1157 Jul 14 08:59 .bash_history
drwx------+ 5 ladmin staff 170 Jul 14 08:33 Desktop
drwx------+ 6 ladmin staff 204 Jun 2 13:48 Documents
drwx------+ 8 ladmin staff 272 Jul 3 18:10 Downloads
drwx------+ 29 ladmin staff 986 Jul 3 17:49 Library
drwx------+ 3 ladmin staff 102 Jun 2 12:53 Movies
drwx------+ 3 ladmin staff 102 Jun 2 12:53 Music
drwx------+ 4 ladmin staff 136 Jun 2 12:53 Pictures
drwxr-xr-x+ 4 ladmin staff 136 Jun 2 12:53 Public
drwxr-xr-x+ 6 ladmin staff 204 Jul 3 18:11 Sites

Create the .bash_profile or .zshrc file using the command line program called ‘nano’ if it doesn’t exist, if it does exist you add your aliases to the end of the file. The commands below assume you are in your home directory:

nano .bash_profile

or…

nano .zshrc

If you are not in your home directory you would use a prefix of the tilde ‘~’ and forward slash, which is the home directory path:

nano ~/.zshrc

or

nano ~/.bash_profile
Bash Alias Osx1

When the .bash_profile or .zshrc file is created you are ready to enter your alias commands.
So here I am using the alias ‘l’ to alias the command ‘ls -lah’

alias l='ls -lah'

In nano ‘control+o’ to write the file out and ‘control+x’ to exit the file.

Refresh the shell environment by entering the command below:

source ~/.bash_profile

Or..

source ~/.zshrc

That’s it, now the alias will take effect.

To add other aliases just start a new line, and apply the same formatting.

For symbolic links or desktop aliases see this reference.

8 Comments

  1. JanHgm on November 1, 2022 at 9:10 pm

    Instead of making a long alias one can better put this such things in a bash script.

  2. Freddy on July 24, 2021 at 4:23 am

    This was “perfect” to find tonight. Thanks for posting this! I’m new to Linux and this MacBook and liking it a lot so it’s time to be more efficient and learning to use aliases is awesome!

    All the best – Freddy

  3. lemmade on June 21, 2021 at 10:07 pm

    Thank you for writing this post! I’ve spent a bit too much of this afternoon trying to make my aliases persist — this was the only effective solution that I was able to find.

  4. Stack on May 9, 2021 at 12:01 pm

    You say “backslash” (\) but then you show a forward slash (/). Can I assume your example is correct and your English verbiage is incorrect? Or should it actually be a backslash?

    • Neil Gowran on May 9, 2021 at 10:44 pm

      Yes correct, and updated in post.

  5. Alberto on April 30, 2021 at 6:24 pm

    Is better open the file on your home directory
    nano ~/.zshrc
    or
    nano ~/.bash_profile

  6. Timothy Smith on April 14, 2021 at 7:18 pm

    I want to create an alias that acts sorta like a macro, so when I enter the alias it just echoes the alias, but does not execute it. Here’s what I want to do: I frequently use “find” to search the folders on my Mac for a list of files that terminate in something like *.JPG. And then do something. So I type in:
    $ find 2>/dev/null ~ -iname “*.JPG” -not -path “*/Library*” -exec cp -a -n {} /Volumes/some folder \;
    I want terminal to print this for me without executing it, so I can go back and modify the command, then hit return to execute it.
    Maybe alias is not what I want. Any other ideas?

    • Chase on May 18, 2021 at 12:52 am

      Hey, here’s an idea for that:
      alias expansion=”echo ‘find 2>/dev/null ~ -iname “*.JPG” -not -path “*/Library*” -exec cp -a -n {} /Volumes/some folder \;'”

Leave all Comment