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
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.