Creating a new alias is very simple! You use the alias command followed by the name you want to create and the command it should run.
Basic Syntax
alias name='command'
Note: Make sure there are no spaces around the equals sign (=).
Example 1: A simple shortcut
If you find yourself moving to a specific folder often, you could create a shortcut:
alias goproj='cd /home/labex/project'
Now, whenever you type goproj, the terminal will immediately change your directory to that path.
Example 2: Adding default flags
If you want the df command (which shows disk space) to always show sizes in a human-readable format (MB/GB), you can do this:
alias df='df -h'
Temporary vs. Permanent
- Temporary: If you type the command directly into the terminal (like the examples above), the alias will only work in your current session. If you close the terminal, it's gone.
- Permanent: To keep your alias forever, you need to add that line to your shell's configuration file (usually a hidden file named
.bashrcor.zshrcin your home directory).
How to remove an alias
If you change your mind, you can use unalias:
unalias goproj
In the context of this lab, understanding aliases helps you realize why type ls gives a different result than type cd! Shall we continue with the lab steps to see how to get help for these commands?