The “bash alias” corresponds to a shortcut containing various commands to be executed in a “bash” environment. It mostly replaces multiple/big commands with the user-defined string. It is quite beneficial to address complex commands that need frequent execution.
This post pens down the complete procedure to create permanent bash alias with the following outcomes:
- Create a Permanent Bash Alias
- Create a Permanent Bash Alias as Function
- Remove a Permanent Bash Alias
How Do I Create a Permanent Bash Alias?
This section carries out the essential steps for creating permanent bash alias quickly.
Step 1: Edit the “~/.bashrc” File
First, open the “.bashrc” file in the text editor “nano” by just specifying its name with the desired file:
$ nano ~/.bashrc
Once the file is opened, please ensure that the above-highlighted lines are in the “.bashrc” file.
Step 2: Create Permanent Bash “alias”
The basic syntax to add an alias is defined below:
$ alias alias_name="command_to_run"
The syntax contains the following parameters:
- alias: Represents the main keyword.
- alias_name: Shows the name of the created alias.
- command_to_run: It is encoded in the inverted commas without any space.
By following the above syntax, we created an alias for the most frequently used commands “update” and “upgrade” having the name “upd”:
#For Update System Packages
alias upd="sudo apt update && sudo apt upgrade"
The “#(hashtag)” symbol denotes the explanation of the alias as an explanation. Press “Ctrl+S” to save and “Ctrl+X” to exit the file.
Step 3: Activate the Bash “alias”
The newly created alias will be present the next time you log in as a new session. But to activate the “upd” alias, instantly execute the “source” command:
$ source ~/.bashrc
The above command is executed successfully.
Step 4: Run the “alias”
Now, simply type the “upd” alias and press the “Enter” key. The defined updation and upgradation job of the “upd” alias will be started:
$ upd
All the system packages have been updated and upgraded successfully.
How Do I Create a Permanent Bash Alias as Function?
The bash alias can also be used as a function in which multiple commands can be executed simultaneously. Follow the few essential steps to perform this task:
Step 1: Create the Bash alias Function
The basic syntax to create the bash alias function is:
function_name () {
[commands]
}
Here we created a new function of the name “tomv”:
- It will first create the file and then move it into the “Downloads” directory.
- The “$1” corresponds to the positional parameter for taking arguments from the command and passing it to the function. Here, the $1 represents the filename:
tomv(){touch $1 && mv $1 ~/Downloads }
Step 2: Append the Function to the “.bashrc” File
Add the above newly created “tomv” alias with the arguments in the “~/.bashrc” to make it permanent:
$ nano ~/.bashrc
Save “Ctrl+S” and exit “Ctrl+X” in the script file.
Reload the “~./bashrc” file to update for making the changes effective in the system:
$ source ~/.bashrc
Step 3: Check the Bash alias Function
Now, execute the alias function “tomv” as a command with the argument (representing the filename) and see the results:
$ tomv
The “tomv” has done its job successfully, and the “itslinuxfoss.txt” file has been moved into the “Downloads” directory after its creation.
How Do I Remove a Permanent Bash Alias?
All the created and default bash alias can be seen by just executing the “alias” in the terminal:
$ alias
Now, remove the “upd” alias from the “.bashrc” file and reload it again to make the changes effective:
$ nano ~/.bashrc #Open “.bashrc” file
$ source ~/.bashrc #Reload “.bashrc” file
Enter “Ctrl+S(save)” and “Ctrl+X(exit)”.
Again execute the “alias” as a command, and now the “upd” alias will not be available in it:
$ alias
The “upd” alias has been completely removed.
Conclusion
To create a permanent bash alias in Linux, edit the “~/.bashrc” file in the text editor(users’ choice). Once the alias is created, reload it using the “source ~/.bashrc” command to activate the new alias. The user can also create an alias in the form of a “function” with many commands.
This guide has illustrated a brief detail to create a permanent bash.