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
Create New Aliases
Creating aliases in bash is easy. The syntax is as follows:
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.
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:
|Linux For Starters: Your Guide to Linux|
|Part 1||What is Linux? Why use Linux? What do I need?|
|Part 2||Choose a Linux distribution meeting your specific needs and requirements.|
|Part 3||Make a bootable Ubuntu USB stick in Windows.|
|Part 4||We show you how to install Ubuntu 21.04 on your hard disk.|
|Part 5||Things to do after installing Ubuntu.|
|Part 6||Navigating your way around the Desktop.|
|Part 7||Updating the system, install new software.|
|Part 8||Open source replacements for proprietary Windows desktop software.|
|Part 9||Get started with the power and flexibility of the terminal.|
|Part 10||We cover the basics of files and permissions.|
|Part 11||Getting help from your system.|
|Part 12||Learn all about the file system.|
|Part 13||Manipulating files from the shell.|
|Part 14||Maintain your system with these simple tips.|
|Part 15||Managing users on your system.|
|Part 16||Explore different desktops to GNOME 3.|
|Part 17||Gaming on Linux.|
|Part 18||Protect your privacy with this guide.|
|Part 19||Access the Windows desktop from Linux using a remote desktop client.|
|Part 20||Set up a virtual machine running Ubuntu as the host and openSUSE as the guest.|
|Part 21||Wine lets you run Windows programs on Linux without emulation.|