Last Updated on May 22, 2022
Aliases
A bash alias is simply a keyboard shortcut, a means of replacing a long command sequence with a shortened version.
A fresh installation of Ubuntu already comes provided with some aliases. They can be viewed with the shell builtin command alias.
As you can see there are 8 aliases available in the shell. So to issue the command ls -alF
, we only need to type la
.
Create New Aliases
Creating aliases in bash is easy. The syntax is as follows:
alias alias_name="command_to_run"
An alias declaration starts with the alias keyword followed by the alias name, an equal sign and the command you want to run when you type the alias. The command needs to be enclosed in quotes and with no spacing around the equal sign. Each alias needs to be declared on a new line.
Let’s say we want to create an alias to make it quicker to search the shell history for specific text. Say we were browsing manual pages with the man command but couldn’t remember the precise manual page we were viewing. We’re going to use the history shell builtin combined with an external command called grep. grep prints lines that match patterns.
First we start nano (a simple text editor) and edit the file .bashrc
$ nano ~/.bashrc
Add the following line to the bottom of the file.
alias gh="history|grep"
Save the file with Ctrl+O and press return. Then press Ctrl+X to exit nano.
Make the new alias available in your current shell by typing:
$ source ~/.bashrc
or close the current shell and start a new one.
In the image below, we’ve run “gh man”. This search the history and prints any line that contains the text “man”.
We’re about to issue the command !133. That’s an exclamation point followed by the number 133.
This instructs the shell to execute line 133 of our history. In this case that’s the command “man whereis”.
Pages in this article:
Page 1 – Types of Commands
Page 2 – Shell Builtins
Page 3 – Aliases
Page 4 – External Commands
Page 5 – Navigate the shell efficiently
Page 6 – Appendix – Explanation of Shell Builtins
All articles in this series:
This is for beginners? Every other word seems undefined. I can read Supreme Court briefs and medical research but not this! The most important thing unexplained is what can be accomplished with these commands. If someone is willing to talk to me to explain these things, I am willing to suggest easier explanations.
Every other word seems undefined?? I think you should read more of the guide before coming to your discourteous comment.
You’ll learn a lot from Part 11, as that shows you how to find what you can do with the commands.
Well, I have not figured out just who you all really are yet….. maybe I won’t be able to>?. But, this is the best “Everything About Linux” I have ever found, on the Internet! And I have been doing Linux in many flavors from 1994! Mostly, I go between Debian and Kubuntu, which I use because of the KDE desktop, which I really like, works great for me to do anything I need!
Wow….. I now have a place to send those I try to talk into using Linux and out of paying that expensive Microsoft tax. I bailed out of Microsoft Windows 95SE in 94′. Furthermore, I have never looked back, and I have kept my money to donate to the many parts of Linux that don’t fly the “Blue Screen Of Death” for a fortune.
Don’t go away anytime soon, I’m going to work even harder now to talk others into dumping Windoz!