How to set an alias in a macOS or Linux shell

By

Learn how to set a shell alias on macOS or Linux, like alias ll='ls -al', and make it permanent by adding it to your .zshrc, .bash_profile, or fish config.

~~~

Sorry, no Windows instructions as I don’t use Windows. Use Google if that’s the OS you use.

I found a funny post on Reddit that highlights how we, as developers, can lose productivity over the small things, like typos:

Set git aliases

I don’t have this set up, as I mostly use a GUI for Git (GitHub Desktop). But I use other aliases. Most notably ll instead of ls -al.

How do I set up an alias in the terminal?

Also check my guides how to use the macOS terminal, the Bash shell and the Bash scripting tutorial.

I said macOS in the title, but this works also on Linux of course.

Here’s the syntax:

alias <newcommand>='<old command>'

Here’s the example I mentioned above:

alias ll='ls -al'

Quote the right-hand side when the command has spaces or flags. Without quotes the shell stops at the first space, so ll becomes just ls (and Bash prints an error about -al):

alias ll=ls -al
# the shell treats -al as a separate argument, not part of the alias

With quotes, ll expands to the full ls -al command.

This works in Bash, Zsh, Fish shell and others too.

If you write this in your shell, from now on ll will be a new available command in the console.

Note: this alias is going to be valid for the entire session, which means until you close the shell, or you restart the computer, whatever comes first.

To see every alias in the current session, run:

alias

To drop one for this session only:

unalias ll

That removes ll until you define it again, or until a new shell loads it from your config. Fish has no unalias: there an alias is a function, so you remove it with functions -e ll.

To persist an alias, so you can use it any time in the future, you need to add it to the configuration file for your shell.

zsh (default on macOS)

Since macOS Catalina the default shell on the Mac is zsh, for licensing reasons (newer Bash versions are GPLv3, and Apple never shipped them). The configuration file is ~/.zshrc, in your home folder:

code ~/.zshrc

~ always points to your home folder path

(assuming you have VS Code installed, which provides the code command).

If ~/.zshrc does not exist, you can create it, and the shell will pick it up the next time you open a terminal.

Bash

If you use Bash, that’s the .bash_profile in your home folder. It’s an invisible file, so you might need to open it with the terminal rather than the Finder:

code ~/.bash_profile

If ~/.bash_profile does not exist, you can create it, and the shell will pick it up. On Linux the terminal usually reads ~/.bashrc instead, so put the alias there.

Fish

The fish configuration is stored in ~/.config/fish/config.fish, and you can add the alias there.

Fish also has a shortcut. Run alias --save ll='ls -al' once and fish writes the alias to a file in ~/.config/fish/functions/, so every new session has it without editing any config file.

Tagged: CLI · All topics

Want me to talk about your product? You can sponsor this site.

~~~

Related posts about cli: