Understanding Bash Aliases in Linux

Postgraduate in Communications Engineering with working experience in the Support Desk and self-study in software development.
Search for a command to run...

Postgraduate in Communications Engineering with working experience in the Support Desk and self-study in software development.
In the context of Linux and Unix-like operating systems, foregrounding and backgrounding are concepts that relate to job control—a way of managing running processes from a shell (command line interface). When you run programs or commands in a shell, ...
The git log command is used to view the commit history of a Git repository. You can customize the output format to show specific details in a more readable or structured way. Below are some useful variations and formatting options for git log: To sh...

Overview This article will teach us how to: Install Ubuntu Server 24.04 on a laptop from scratch Install Ubuntu server by using Vagrant tool with ISO image Prerequisites: A laptop without any OS installed; A virtualization software Virtualbox t...

The /etc/passwd and /etc/shadow files are the backbone of Linux user management. Together, they store user account information and handle authentication securely. This article provides a hands-on guide to understanding these files, their structure, a...

In Linux, links are powerful tools that allow you to create references to files and directories. There are two main types of links: hard links and soft links (also known as symbolic links or symlinks). Understanding the differences between these two ...

A hostname is a human-friendly name given to a computer. It is a unique identifier that allows us to identify the machine in various network communications, making it easier to locate and manage devices. Type: hostname To see where it is stored, typ...

In Linux, aliases are shortcuts that replace long or frequently used commands with shorter, more convenient ones, improving efficiency by speeding up the workflow and avoiding potential spelling errors. They can also replace commands with additional options, making them easier to use.
Save time by typing less.
Improve command memorability through custom names.
Increase efficiency by automating repetitive tasks.
To create an alias, use the alias command and its syntax as follows:
alias [name]='[value]'
alias: Invokes the alias command.
[name]: the name of the alias is being created. This is what will be typed in the terminal instead of the full command. A name is a user-defined string, excluding special characters and 'alias', 'unalias' keywords that cannot be used as names.
[value]: the actual command that the alias represents. This can be a simple command or a more complex command chain. Commands can also include options, arguments, and variables.
There are two types of aliases to create in Linux:
Temporary. Add them using the alias command.
Permanent. These require editing the system files.
Aliases defined directly in the terminal are temporary and last only for the current terminal session. Use the alias command to create a temporary alias. For example, adding move as an alias for the mv command with the option of asking for confirmation before overwriting:
alias move='mv -i'
Another use for aliases is to create a shortcut for running scripts. To do this, provide the absolute path to the script as the value:
alias frename='Example/Test/file_rename.sh'
In this example, using frename as a command runs the file_rename.sh bash script.
To make an alias permanent, add it to the shell's configuration file, such as ~/.bashrc or ~/.bash_profile, and then source the file or restart your terminal. Depending on the type of shell you are using, use:
Bash shell: ~/.bashrc
Zsh shell: ~/.zshrc
Fish shell: ~/.config/fish/config.fish
Start by opening the shell configuration file in a text editor. In this example, we are using the Bash shell and vim text editor:
sudo vim ~/.bashrc
Scroll down until you find a section that lists default system aliases. For ease of use, create a separate section with a descriptive comment and add your aliases using the alias command syntax.
In our example:
#Custom aliases
alias c='clear';
alias df='df -h -x squashfs -x tmpfs -x devtmpfs'

Once all of the new aliases are added, press ESC, type :wq! to save the changes to the configuration file.
The new aliases automatically load in the next terminal session. If you want to use them in the current session, load the configuration file using the source command:
source ~/.bashrc
The output of the df command before creating an alias for it:

The output of the df command after creating an alias for it:

Using the alias command on its own displays a list of all currently set aliases:
alias

To remove an alias, use the unalias command with the following syntax:
unalias [name]
Adding the -a option allows to remove all aliases:
unalias -a
