Linux for Starters

Linux for Starters: Your Guide to Linux – Get Started with the Terminal – Part 9

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.

Linux for Starters - aliases

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”.

Linux for Starters - alias

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”.

Page 4 – External Commands

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

Share your Thoughts

This site uses Akismet to reduce spam. Learn how your comment data is processed.