When you first start using Linux, you’ll need only a handful of commands. As you gain experience, you learn more of them, but the complexity of the commands increases. Commands like awk, df, git, etc., all have a huge range of options and endless combinations of them. Every Linux system administrator and every server has its own peculiarities. The commands that work for you won’t feel natural for someone else, so you develop your style, workflow, and way of doing things. With such specificity, comes complexity, and you should know how to use the bash alias to streamline this.
What is a Bash Alias?
A bash alias is a substitute for a command. You can use it to substitute commands of arbitrary complexity. While the basic “alias” command doesn’t support arguments, you can create aliases using functions instead. I’ll show you how to create both. Here are the main benefits of aliases.
Saving Time for Repetitive Commands
There are many commands that you use a lot but can be a pain to type. For example, everyone uses “ls” to list the contents of a directory, but you often want to see everything, including hidden files, and see all the permissions and space used by each file in a human-readable format. The command for doing this is:
ls -lah
Not too hard to type, of course, but nowhere near as easy as simply “ls”. And that’s an easy example. Some statements involving grep or awk can become incredibly complicated with special characters, curly brackets, and more. Using an alias is a great way to save time and replace a complex command with a simple one.
Reducing Errors and Increasing Safety
Complex commands can include typos, which fail the command. Long commands with special characters are particularly vulnerable to this. But the command failing is the least of your worries. The worst that can happen is you feel frustrated and have to find and correct the mistake.
Far more dangerous are those commands where a typo can result in disastrous consequences. For example, commands like “cp” and “mv” will quietly overwrite any files with the same name. For this reason, we often use these commands with the “-i” flag, like this:
cp -i filename.txt
The “-i” asks for confirmation if you try and overwrite a file. But it’s possible to forget this argument. Or maybe someone new is using your server and they don’t have the caution to use the “-i” flag each time. This could result in a disastrous wipeout of an important file.
An alias can enforce the usage of the “-i” flag by redefining the “cp” command to always include it.
Improved Readability
Using an alias can greatly improve the readability of a complex command. For example, take the following git command:
git log --oneline --graph --decorate --all
This will show a neat, visual representation of the commit history. But who is going to sit and type all this each time – particularly if it’s a commonly used command? Not to mention it’s easy to forget to include a particular flag. It’s far easier to create an alias with a clean name that can replace this.
For all the above reasons, an alias is one of the most powerful tools in the arsenal of an experience Linux administrator, and it allows them to customize the shell to their own unique needs.
Now let’s see how we create these aliases.
How to Create Aliases in Bash
To create a bash alias, we simply use the following command:
alias shortname='command'
So for example, to create an alias of the “ls -lah” command I showed earlier, we just use:
alias ll='ls -lah'
After you run the above command, you can just type “ll” and it’ll expand to the full command. Here’s how it looks:
As you can see, if this is a command that you use often, then having an alias makes your job much easier.
You can also use aliases to chain commands together. In Linux, often you use multiple commands one after the other in quick succession. For example, when you’re installing a new package, you always want to ensure that you have the latest repository information. This means you should both “update” and “upgrade” the repos one after the other. Typing out both these commands each time is annoying, and you can use an alias like this:
alias update='sudo apt update && sudo apt upgrade -y'
Here’s how it works:
So with this alias, you can just type “update”, and the command will execute. See how much easier this is than typing out commands one after the other! As a bonus, you won’t have to wait before the first command is done before moving on to the next since they’ll start automatically one after the other.
Making the Aliases Permanent
Unfortunately, when you create a new alias in Linux as shown above, it’s only temporary. The aliases will vanish once you exit and restart the shell. This is by design – the temporary aliases are stored in working memory and aren’t written in the shell configuration. If you want your definitions to persist, then you must write the changes into the following file:
~/.bashrc
The above file is the place where you can place all the custom configurations for the current shell. You can open this file for editing using the following command:
vi ~/.bashrc
Here’s what the contents of ~/.bashrc look like:
As you can see, I already have some aliases defined such as “ll” for “ls -la” and I’ve redefined “ls” to include the “–color=auto” flag. If you have additional aliases, make sure you add them to this file and save your changes.
But there’s one more step. The changes to the ~/.bashrc file will take place when you restart your shell – they won’t be applied automatically to the current shell. If you want this to take effect immediately, you can use the “source” command like this:
source ~/.bashrc
This will apply the changes, and you can start using the aliases defined in .bashrc.
Can’t Use Aliases with Dynamic Arguments
While you can use an alias with static arguments that work when the command is fully expanded, it doesn’t work with dynamic arguments. For example, let’s say I want to search all “txt” files for a word. The regular command I use is:
grep "[word]" *.txt
Now I try and create an alias for a new command like this:
alias mygrep='grep $1 *.txt'
The hope is that I can type:
mygrep hello
Unfortunately, this won’t work, because the “alias” command will simply expand blindly and won’t replace $1″ with “hello”. To work around this, we can use a function instead. So something like this will work:
mygrep() { grep "$1" *.txt; }
Now when we run:
mygrep hello
It’ll give us the desired expansion. So despite functions not strictly belonging to the “alias” keyword, the utility is similar, so everyone using aliases should be aware of how powerful functions can be in this regard.
Conclusion
As you can see, a bash alias can be extremely useful for all kinds of scenarios involving Linux administrators. Hopefully, you can use some of the ideas I’ve presented to craft your own, unique aliases that will save you time.

I’m a NameHero team member, and an expert on WordPress and web hosting. I’ve been in this industry since 2008. I’ve also developed apps on Android and have written extensive tutorials on managing Linux servers. You can contact me on my website WP-Tweaks.com!
Leave a Reply