Making an Alias

Now that you’ve learned how to use the editor of your choice (Nano, Emacs, or Vi), you’re ready for your first real foray into being a Unix command line expert.

You’re going to make an alias. An alias is a short code or set of characters you will type on the terminal that will do something else. Specifically, it will be an alias (or a “stand-in”) for another command. Typically the command being stood in for is longer and would be tedious to write. You will make an alias for a common task— something you do every day — to make your life easier.

Making an Alias in Bash

Open the file ~/.bashrc using the editor of your choice, like Nano:

nano ~/.bash

Remember that ~ means the home directory and . in this context means a hidden file, because it is the first letter of the file’s name.

Making Alias In ZShell

Open the file ~/.zshrc using the editor of your choice, like Nano:

nano ~/.zshrc

Notice that the ~ means the home directory, as we learned about in The File System and pwd

Next, move to the end of the file and add this line:

alias sayhello="echo 'hello'"

Where does this line go?

Save and exit the editor.

Confirm the file you just edited has the changes. The reason you do this is because you are just getting used to using the editor.

You do this using more command you learned about in the lesson on cat, more, and echo

more ~/.bashrc (for Bashrc)

more ~/.zshrc (for Zsehll)

Your output should look like this:

Now, type your new alias at the command prompt. Notice that it does not work.

% sayhello

That’s because changes to the ~/.bashrc or ~/.zshrc file you edited does not get picked up automatically.

To make the terminal you’re in have the new alias, you have two choices:

  1. Source the ~/.bashrc or ~/.zshrc file
  2. Open a new terminal window

Let’s try option #2 first

Open a new Terminal window. Opening a new terminal window automatically sources that file into the new Terminal window. But notice if you go back to the first terminal window, the sayhello alias is still not available.

Let’s try option #1:

Back in the first terminal window (the one where your alias does not work), type source ~/.bashrc or source ~/.zshrc

This will load that file (and execute its shell scripts) within your current terminal.

Once you do that, typing sayhello and watch it work.

You can do anything in an alias that you can do in a shell. You are encouraged to make shortcuts for yourself for your most common repetitive tasks; don’t go overboard. I use shortcuts to move between projects, lint my code, and other very common things I need to do all day.