/

How to Set an Alias in a macOS or Linux Shell

How to Set an Alias in a macOS or Linux Shell

In this article, I will show you how to set an alias in a UNIX environment, specifically in shells like Bash, Zsh, and Fish, on both macOS and Linux. Note that this guide does not cover Windows instructions.

I recently came across a funny post on Reddit that highlighted how developers can lose productivity over small things like typos. Here’s the post:

Set git aliases

While I personally prefer using a GUI for Git (GitHub Desktop), I do find aliases useful. For example, I often use the ll alias instead of typing ls -al.

So, how do we set up an alias in the terminal? Here’s the syntax:

1
alias <newcommand>='<old command>'

For instance, to set the ll alias to ls -al, you would use:

1
alias ll='ls -al'

This syntax works for Bash, Zsh, Fish, and other shells as well.

Once you’ve set the alias in your shell, you will be able to use the ll command in the console.

It’s important to note that the alias will only be valid for the current session. This means it will be available until you close the shell or restart your computer, whichever comes first.

To make the alias persistent and available for future use, you need to add it to the configuration file for your shell.

If you’re using Bash, the configuration file is .bash_profile in your home folder. Please note that this file is hidden, so you might need to open it using the terminal instead of the Finder. Here’s an example command to open the file using VS Code (assuming you have it installed):

1
code ~/.bash_profile

If ~/.bash_profile does not exist, you can create it, and the shell will recognize it. Alternatively, you can use the ~/.bashrc file if it’s present.

For macOS Catalina users, the default shell is Zsh due to licensing reasons. In this case, the configuration file is ~/.zshrc in your home folder. You can open it using the following command:

1
code ~/.zshrc

For Fish shell users, the configuration file is located at ~/.config/fish/config.fish. However, it’s more convenient to configure it using the fish_config command.

I hope this guide helps you set up aliases in your macOS or Linux shell. For more information, you can also check out my guides on how to use the macOS terminal, the Bash shell, and the Bash scripting tutorial.

Tags: alias, terminal, macOS, Linux, shell, Bash, Zsh, Fish