Skip to main content

How to use the Alias Command in Linux

Introduction

Aliases are shortcuts that can help you in your day-to-day work. How you add aliases depends on the shell you use. In this blog post I will explain to you how to add aliases for the default bash shell. Everything is tested on Debian 10.

List currently defined aliases in Linux

$ alias


Types of Aliases in Linux

There are two types of aliases: temporary and permanent. The temporary aliases are available just for the current terminal session. You can't use those aliases in another terminal session and they will be deleted when you close the session. Temporary aliases are useful if you need an alias for a short time. But if you need a command daily, is really complex, and/ or need it in multiple terminal sessions, a permanent alias is needed/ recommended.

Create command-line aliases

Temporary alias
  • $ alias yourAlias="your command" keep in mind: case-sensitive

It will be available immediately in this session. You can try it or retype alias to get the list of defined aliases.

Permanent alias
  • echo 'alias yourAlias="your command"' >> ~/.bash_aliases

OR

  1. Open ~/.bash_aliases in your favorite text editor (if the file doesn't exist, create it)
  2. Add a new line with your alias: alias actualyAlias="your command"
  3. Your new alias will be available for all new sessions. For active sessions you can type source ~/.bash_aliases to reload your new alias.

  4. You could add your aliases to ~/.bashrc, but the method above prevents you from destroying your ~/.bashrc file and keeps your custom aliases separated.


Remove aliases

To remove aliases from your current session you can use the unalias command.

unalias yourAlias - Deletes one specific alias in this session

unalias -a - Deletes all aliases in this session

To delete an alias permanently you have to remove the alias from your ~/.bash_aliases/ ~/.bashrc file.


Execute alias as sudo

Before you can use aliases as a sudo user, you have to add sudo="sudo " to your aliases.

alias sudo="sudo "


Multiple commands in one alias

Executes in serial, even if the previous command failed

  • alias yourAlias1="cmd1;cmd2;cmd3;"

Executes in serial, but only continues if the previous command succeeded.

  • alias yourAlias2="cmd1 && cmd2 && cmd3"

Executes parallel

  • alias yourAlias3="cmd1 & cmd2 & cmd3"

Some examples

Debian update routine

alias update='sudo apt-get update && sudo apt-get upgrade'

Initiate ssh session

alias ssh_ccr="ssh user@123.241.1.1"

Push dev env to a webserver

alias push2prod_ccr="rsync -av --update /devenv/ccr/ user@123.241.1.1:/home/web/html --progress


Conclusion

It is really easy to create and manage aliases and they help me with my daily tasks. Give it a try and let me know how you use them.

If you want to share your feedback, questions, or just want to reach out, feel free to contact me via Twitter or Mastodon.