Bash Aliases
If you’re not familiar with bash aliases I highly recommend you read on. Bash aliases are fantastic for saving time and not having to remember commands or groups of commands.
A bash alias is just a shortcut that you can set to be a word or even a single letter.
The syntax for an alias is as follows:
alias shortcut='value'
- alias = Command used to set an alias per session.
- shortcut = The word or letters you choose to call your commands or groups of commands.
- value = The commands you want to call.
There’s a few ways to use a bash alias. You can set the alias to call a script, command, or group of commands.
An alias can be set per session, meaning that if you logout or reboot, the alias is removed. You can also set an alias permanently which is how I like to set mine.
Temporary Bash Aliases
Setting up an alias for the current session is straightforward. This method is usually good for setting up an alias that you don’t need permanently.
In this example we set an alias for the clear command. Just enter this in your terminal:
alias c='clear'
Now we just need to type “c” in the terminal to clear any output.
c
If you decide you want to remove the alias, you can run the following command:
unalias c
This removes the previous alias we set for the clear command.
Permanent Bash Aliases
This is my preferred method to add a bash alias. I generally set aliases that I often use.
To add an alias permanently, you would add it to your “.bashrc” file. You can do this by opening the file in your text editor and adding the aliases at the bottom of the file.
To add the alias for the clear command, you can add the following to your bashrc:
# My aliases
alias c='clear'
Don’t forget to restart your terminal to start using your new aliases.
Note: We added the “My aliases” comment just for future reference.
You can also add the alias from terminal as follows:
echo "# My aliases" >> .bashrc
echo "alias c='clear'" >> .bashrc
To delete an alias, you can just delete it from the bashrc file.
View Bash Aliases
To view all aliases you can just run the alias command. This can be very useful if you ever forget an alias.
Just run the following:
alias
Sample Bash Aliases
Here’s some samples of aliases just to give you some ideas. You can really get creative with aliases and handle large tasks with as little as entering a single letter in your terminal.
Reset your terminal:
alias r='tput reset'
Update your system:
alias update='sudo apt update && sudo apt upgrade -y'
Call a bash script:
alias scpt='/path/to/script.sh'
Conclusion
Bash aliases in linux are a must have. Aliases just makes using the terminal much quicker and efficient and I’m all for that.
That’ll be all for this look at bash aliases. If you have any aliases you use regularly that you’d like to share, please do so in the comments below.
Tags: tutorials, linux, command line