Bash Scripting - Working of Alias
Last Updated :
11 Feb, 2022
BASH Alias is a shortcut to run commands using some mapping. It is a way to create some shortcut commands for repetitive and multiple tasks. We create an alias in a BASH environment in specified files. We can also incorporate BASH functions, variables, etc to make the Alias more programmatic and flexible.
What is a BASH Alias?
A BASH Alias is a map of commands with the sets of commands or functions that can be used as a shortcut in the command line for a BASH environment. Bash Alias allows to aggregate multiple functions into a single command and also it avoids repetitive or large commands into a simple shortcut command. BASH Alias is a map of shortcut command with the sets of commands which are stored in a file called either .bash_profile or .bashrc with a specific syntax. The aliases can also be combined with the BASH syntax and semantics to make the shortcuts more customizable and flexible.
Working of an Alias
The alias keyword replaces the command with the string which might be sets of commands or functions. The alias is defined in the ~/.bashrc or ~/.bash_profile. These files are loaded in the shell environment and thus the commands listed in the alias are also been loaded and ready to be executed. Thus, the alias work with the BASH configuration.
After the aliases or command maps have been loaded, the user can interact and enter the commands. If the command in the alias is executed the BASH interpreter replaces the command alias with the string of commands and hence the desired functioning of the alias or command is achieved.
We'll see how to create a BASH Alias in the next section.
How to create an Alias?
To create an alias, we need to create a file called bash_profile or bashrc. This file needs to be in the root directory and should be hidden to avoid mishandling of its contents. Inside of this file, we create an alias which is basically a map of commands and the sets of commands/functions to be executed as a shortcut.
Creating .bashrc or .bash_profile
Firstly, you can locate to your root directory, open up a terminal or command prompt and type in the following command:
For Linux/macOS:
cd ~
For Windows:
If using git bash terminal or Powershell
cd ~
You should be now into so-called the root directory of your environment.
After moving to the root directory, you need to create a file called .bashrc or .bash_profile in the root directory itself. You can create the file however you create the file as per your preferences but to keep a standard way for most of the operating systems the following command will work:
echo >> .bash_profile
OR
echo >> .bashrc
This will create a file if not present but won't do anything if the file is already created and has content.
If you don't like to move into the root directory then create and write to the file, you can access the root directory with the "~/". You can create the file being in any other directory or Path in your OS. The following command creates the file without changing your current directory.
echo >> ~/.bash_profile
Creating a function
We can create an independent function in our bashrc or other bash scripts and further link those with a shortcut command by calling them. To create a function in BASH, we can use a simple syntax as follows:
function x()
{
# statements
}
OR
x()
{
# statements
}
We can then, write the logic for the command in this function block. For this example, we can create a function to create a directory and cd or move into that directory. Hence, we can write the code inside the function block like so:
function x()
{
mkdir $1
cd $1
}
The $1 is a positional parameter to take arguments from the command line and parse it to a function or in a script as a variable. You can learn about them in this article. Simply put, they make the script or the alias more dynamic and allow easier user input directly from the command line.
Creating the BASH alias
Now, we have the bashrc or bash_profile created, we can start with creating aliases. To create an alias we will require three things, the alias keyword to state that the following sentence will create an alias, the command or a shorthand word to be used for running the last piece i.e. the commands which are to be executed. The commands inside an alias need to be executable from the Shell/ BASH environment.
The syntax of an alias is as follows:
alias command="sets of commands/functions"
As said earlier, we require three pieces of information i.e. the alias keyword, the shorthand command, and the sets of commands or functions to be executed.
How to set a Bash Alias?
We can access the value of the last command as a return type by using the $? outside the function after the function call. But to just execute commands we can simply call the functions
Let's take an example to create an alias, suppose we want to create a directory and move into it, We will have to run two commands i.e. mkdir and cd. This can be a scenario to create an alias. We can chain up commands into a function or alias commands to avoid typing all commands.
The alias would look like this:
alias md='x(){ mkdir "$1"; cd "$1"; }; x'
The alias keyword indicates the BASH interpreter to load the map variable as a command. The command is named "md" which is mapped to a function "x". The function x creates the directory with the name passed to the md command and changes the directory into it. The function is defined and called in the end so as to keep it simple and straightforward.
The other way to do the same trick is to create a function independently elsewhere and call from the alias. The following alias dies the same thing but written in a different way:
alias md='x'
x(){
mkdir $1
cd $1
}
This looks a lot readable and clean. This can also be used to add BASH syntax and semantics like loops, conditions, and other programmatic approaches to make the alias more customizable and flexible.

Difference between single and double quotes
The single and double quotes used in the alias statements have some importance and need to be used as per requirement. We use double quotes to expand the value of the variable in BASH i.e to actually parse in the value of the variable name. If we just use single quotation marks, the variable's value is not displayed and just the variable-name is parsed as it is.
For example in the statement :
#!/bin/usr/env bash
p=125
echo "The value of variable p is $p"
echo 'The value of variable p is $p'
In this example, the value of the variable p is only expanded or parsed in the command if we include it inside the double quotation marks. This also applies when working with an alias in the bashrc or bash_profile files, we need to use double quotations to expand the value of the variable.
Structure of an Alias
After creating an alias we can understand the structure of an alias in a bit of depth. As discussed earlier, we have three parts in creating an alias:
alias keyword
The alias keyword in BASH replaces a string with another string. The shorthand command is the string that will be used instead of the string on the right-hand side in the command. Hence the alias keyword acts as an abbreviation for a string which is a set of commands or functions.
command
The command is the shorthand that will be used in the terminal. We can use any command or string avoiding the actual commands like cd, mkdir, rmdir, touch, echo, etc which might conflict with them and result in undesired behavior.
commands/functions
These are the sets of commands that will be executed when the command in the alias is entered. This can be BASH functions, shell commands, or any other sets of commands that are compatible with the Shell environment.
Similar Reads
Non-linear Components
In electrical circuits, Non-linear Components are electronic devices that need an external power source to operate actively. Non-Linear Components are those that are changed with respect to the voltage and current. Elements that do not follow ohm's law are called Non-linear Components. Non-linear Co
11 min read
Spring Boot Tutorial
Spring Boot is a Java framework that makes it easier to create and run Java applications. It simplifies the configuration and setup process, allowing developers to focus more on writing code for their applications. This Spring Boot Tutorial is a comprehensive guide that covers both basic and advance
10 min read
Class Diagram | Unified Modeling Language (UML)
A UML class diagram is a visual tool that represents the structure of a system by showing its classes, attributes, methods, and the relationships between them. It helps everyone involved in a projectâlike developers and designersâunderstand how the system is organized and how its components interact
12 min read
Steady State Response
In this article, we are going to discuss the steady-state response. We will see what is steady state response in Time domain analysis. We will then discuss some of the standard test signals used in finding the response of a response. We also discuss the first-order response for different signals. We
9 min read
Backpropagation in Neural Network
Back Propagation is also known as "Backward Propagation of Errors" is a method used to train neural network . Its goal is to reduce the difference between the modelâs predicted output and the actual output by adjusting the weights and biases in the network.It works iteratively to adjust weights and
9 min read
Polymorphism in Java
Polymorphism in Java is one of the core concepts in object-oriented programming (OOP) that allows objects to behave differently based on their specific class type. The word polymorphism means having many forms, and it comes from the Greek words poly (many) and morph (forms), this means one entity ca
7 min read
3-Phase Inverter
An inverter is a fundamental electrical device designed primarily for the conversion of direct current into alternating current . This versatile device , also known as a variable frequency drive , plays a vital role in a wide range of applications , including variable frequency drives and high power
13 min read
What is Vacuum Circuit Breaker?
A vacuum circuit breaker is a type of breaker that utilizes a vacuum as the medium to extinguish electrical arcs. Within this circuit breaker, there is a vacuum interrupter that houses the stationary and mobile contacts in a permanently sealed enclosure. When the contacts are separated in a high vac
13 min read
AVL Tree Data Structure
An AVL tree defined as a self-balancing Binary Search Tree (BST) where the difference between heights of left and right subtrees for any node cannot be more than one. The absolute difference between the heights of the left subtree and the right subtree for any node is known as the balance factor of
4 min read
Linux Commands Cheat Sheet
Linux, often associated with being a complex operating system primarily used by developers, may not necessarily fit that description entirely. While it can initially appear challenging for beginners, once you immerse yourself in the Linux world, you may find it difficult to return to your previous W
13 min read