Aliases explained ...
Aliases are essentially shortcuts that can save you from having to remember long commands and eliminate a great deal of typing when you are working.
For example, you could set the alias tsm
to be a shortcut for the transmission-remote
command.
This article explains how to create aliases so you can be more productive on the command line. Chances you are already using aliases on your Linux system.
List Currently Defined Aliases in Linux
You can see a list of defined aliases on your profile by simply executing alias command.
$ alias
Here you can see the default aliases defined for me in Ubuntu server.
gwip='git add -A; git rm $(git ls-files --deleted) 2> /dev/null; git commit --no-verify --no-gpg-sign -m "--wip-- [skip ci]"'
heroku='nocorrect heroku'
history=omz_history
hpodder='nocorrect hpodder'
l='ls -lah'
la='ls -lAh'
ll='ls -lh'
ls='ls --color=tty'
lsa='ls -lah'
As you can see, executing the last command...
$ lsa
...is equivalent to running:
$ ls -lah
How to Create Aliases in Linux
Creating aliases is relatively a quick and easy process.
You can create two types of aliases – temporary and permanent.
We will review both types.
Creating Temporary Aliases
The syntax is as follows:
$ alias shortName="your custom command here"
An alias declaration starts with the alias
keyword followed by the alias name, an equal sign, and the command you want to run when you type the alias. The command needs to be enclosed in quotes and with no spacing around the equal sign. Each alias needs to be declared on a new line.
Here is an actual example:
$ alias wd="cd /var/www"
You can then use "wd"
shortcut to go to the webroot directory.
The problem with temporary aliases is that they will only be available for your current terminal session. A temporary alias is one that will be active only as long as your current shell is active. When you log out and come back later, these aliases are no longer in effect.
If you wish to save your aliases across sessions you will need a permanent alias.
Creating Permanent Aliases
To keep aliases between sessions, you can save them in your user’s shell configuration profile file.
This can be:
- Bash – ~/.bashrc
- ZSH – ~/.zshrc
- Fish – ~/.config/fish/config.fish
The syntax you should use is practically the same as creating a temporary alias. The only difference comes from the fact that you will be saving it in a file this time. So for example, in Z shell, you can open the .zshrc file with your favorite editor.
$ nano ~/.zshrc
Find a place in the file, where you want to keep your aliases. For instance, you can add pile them up at the end of the file.
For organizations purposes, you can leave a comment before your aliases.
#My custom aliases
alias home="ssh -i ~/.ssh/mykep.pem monkey@192.168.0.111"
alias ll="ls -alF"
Save the file. The file will be automatically loaded in your next session. If you want to use the newly defined alias in the current session, issue the following command:
$ source ~/.zshrc
Removing Permanent Aliases
Removing aliases is as simple as adding them. To remove an alias from your current session, issue the unalias
command followed by the alias which you wish to remove as such:
$ unalias alias_name
$ unalias -a [remove all alias]
By now you should have a good understanding of how to create bash aliases that will make your life easier and more productive.
Cheers :~)