Introduction to Bash Alias
In the article of bash alias, we would first look into the meaning and intention of the feature in Bash. Understanding the intention will open up avenues to understand where in real life you would be able to use the particular feature. Bash alias is a methodology where one supplements or overrides particular custom bash commands. The intention behind doing this is to keep us at bay in typing long commands. Using alias one can easily bring down long commands to as small as a word. In doing this one might be able to reduce the time and effort in repeatedly writing the command again and again. In this article,, we will look into a few such methodologies along with examples that help us achieve the desired results. And with this, we also have some examples to take you through real-life scenarios as well!
The alias declaration is pretty straight forward when it comes to bash. In simple lines, we sum up the syntax as:
Here, nameOfAliasToUse is nothing but the shortened version of the command which one needs to execute when they use this name. For example, it can be as naïve as using cl instead of clear in making it perform the tasks clear is supposed to do. In double quotes, one needs to give the command which one expects to run when the nameOfAliasToUse is entered in the bash terminal. In our example, we can confidently say nameOfAliasToUse is replaced by cl, and CommandToRun is replaced by clear.
At first, you would need to make a file named .bash_aliases by typing in touch ~/. bash_aliases and then make sure that this alias file is accessible to bashrc by typing below command in the bashrc file:
if [ -e $HOME/.bash_aliases ]; then
Post this make sure all alias you would use is put under the file .bash_aliases
How Does Alias work in Bash Scripting?
There are potentially 2 ways in which bash alias is done in bash and in this section, we would look at the syntax before moving to the next section of their working in detail. So, here we go:
1. By Changing in the Bashrc File
This method needs to be used in the cases when you rather want the alias to be permanent in nature. In these cases, you would need to open the bashrc file you would need to type
nano ~/.bashrc OR vi /.bashrc
or for that matter of fact any editor you are comfortable with. This editor will enable you to edit the bashrc and input the required alias in the file itself. For future reference, as a rule of thumb, it is always recommended to note the description as a part of the aliasing. This will help us remember the utilization of the alias in future times. Here as a matter of high importance, we would be using the alias name in a way that is most convenient for us to use. For example, if we have to print my IP address every time we run a command, we would rather choose to alias that as a short alias name like printIP. The syntax will be:
alias printIP='curl ipinfo.io/ip'
2. By Making a Function
The next way we would talk about is the creation of bash alias by creating functions. In some or the other utilities, we would need to create an alias that might take arguments as well. This is where creating functions is very handy. This methodology doesn’t necessarily have to have a keyword alias, but more like a function that will take in arguments. These function names can be interpreted as an alias name and used appropriately. The syntax goes as:
[set of commands] }
Here the function_name is replaced by the alias name you would be like to use in the script going forward. The set of commands mentioned here will particularly have all the required commands to fulfill the requirements of the utility. These commands can also be a part of the bashrc file in case one wants to make these alias or function permanent and frequently used functionality.
With the above methodology, we particularly see that aliasing comes in very handy when one needs to continually use the same command over and over again and that command itself is a few characters long. Aliasing is also convenient for developers as they can use their own “code word” for naming them and hence easier to use during the scripting.
Example to Implement Bash Alias
Once here, we will look at a real-life problem statement, and we would solve this by showing it as a part of an example. For the sake of simplicity, we have kept features in the code minimalistic, but keeping the thinking behind for solving will be real-life one! In this section, we will talk about how we use an alias and also some important aliases which will come in handy during scripting!
In our bashrc file you would need to insert following alias:
alias ll='ls -lh'
alias lsize='ls --human-readable --size -1 -S --classify'
alias lastMod='ls -t -1'
alias nowdate='date +"%d-%m-%Y"'
alias openport='netstat -tulanp'
alias top10CPU='ps auxf | sort -nr -k 3 | head -10'
alias top10mem='ps auxf | sort -nr -k 4 | head -10'
mkdir -p -- "$1"
Now as a script we would run the script containing the commands and see the result right in front when used the alias name.
The script is as follows:
echo "Introduction to aliasing"
echo 'The syntax is alias nameOfAliasToUse="CommandToRun"'
echo "To find out list of files in the courrent working directory"
echo "List file and arrange it by size"
echo "Get all the files which were modified yesterday"
echo "Get the date now"
echo "Get all the open ports"
echo "Get the top 10 process sorted in descending order of CPU utilization"
echo "Get the top 10 process sorted in descending order of memory utilization"
echo "Make a new directory"
With the above example, we are clear with the syntax which goes into the making of alias along with the intention of having this article. We also looked at some widely and frequently used alias and hopefully, you would find them handy in the script development!
This is a guide to Bash Alias. Here we discuss the Introduction of Bash Alias and its Syntax along with Examples and its Code Implementation. You can also go through our other suggested articles to learn more –