Home > Tutorials > Linux Server Tutorials > The BASH Shell: Command History & Aliases

The BASH Shell: Command History & Aliases

Command History

Bash keeps track of a certain number of previous commands that you have entered into the shell. The number of commands is given by a shell variable called HISTSIZE (which we will discuss later on). Bash stores the text of your previous commands in a history list. When you logon to to your account, the history list is initialized from a history file. The filename of the history file is .bash_history by default, however this can be customized using the HISTFILE bash variable.

Bash offers us several ways of recalling our previous commands. The simplest way is to use the up- and down-arrow keys, which will scroll through the commands that have been previously entered. Pressing the up-arrow key will cause the last command that was entered to appear on the command line. Pressing the up-arrow key again will put the command previous to that one on the command line, and so on, and so on, and so on. Once you’ve found the command you wish to use, you can make simple edits to it by using the left and right arrow keys to move along the command line. You can insert text at any point in the command line, and can also delete text via the Backspace or Delete key.

Another method of utilizing the command history is to display and edit the list using the history command built into bash. The history command can be invoked using 2 different methods. The first method uses the command: history n

When the history command is used on it’s own, the entire contents of the history list are displayed. However using the n with the history command causes only the last n lines in the history list to be shown. So, history 5 would display only the last 5 commands.

Aliases

Another way that bash makes life easier for you is by supporting command aliases – commands that the user can specify. Usually alias commands are simply abbreviations of other commands, designed to save keystrokes. For example, if you are entering the following command on a regular basis, you might be inclined to create an alias for it to save yourself some typing.

cd /usr/abc123/home/lib/config

Instead of always typing out this command you could create an alias called goconfig that would in-turn cause the longer command to be executed. To setup an alias like this you must use bash’s alias command.

alias goconfig='cd /usr/abc123/home/lib/config'

Now, until you exit from bash, the goconfig alias will cause the original command to be executed. If for some reason you decide you no longer need the alias – you can use the bash unalias command to delete it:

unalias goconfig

Take note that when defining aliases you can’t include spaces on either side of the equal sign or the shell can’t properly determine what you want to do. Quotation marks are necessary only if the command within them contains spaces or other special characters.

If you simply enter the alias command on it’s own – without any arguments, it will display all of the aliases that are currently defined.

About Michael Boguslavskiy

Michael Boguslavskiy is a full-stack developer & online presence consultant based out of New York City. He's been offering freelance marketing & development services for over a decade. He currently manages Rapid Purple - and online webmaster resources center; and Media Explode - a full service marketing agency.

Check Also

How To Populate A DIV via an HTML SelectBox, PDO, and jQuery

Imagine you have a database full of information, and you want to let users browse ...

Like every other website, this site uses cookies to analyze our traffic. Cookies may also be utilized by our advertisers and partners. By using this website you agree to the use of said cookies. More Information

The cookie settings on this website are set to "allow cookies" to give you the best browsing experience possible. If you continue to use this website without changing your cookie settings, continuing to navigate past this message, or you click "Accept" below then you are consenting to the use of cookies on the Rapid Purple website.

Close