Nifty app offers expanded access to terminal history
Reusing Previous Commands with the Bash History Suggest Box
![© Lead Image © lightwise, 123RF.com © Lead Image © lightwise, 123RF.com](/var/linux_magazin/storage/images/issues/2014/164/bash-history-suggest-box/123rf-lightwise.png/618145-1-eng-US/123RF-lightwise.png_medium.png)
© Lead Image © lightwise, 123RF.com
You can always browse the Bash history using your arrow keys, but Bash's search capabilities are very limited. Enter the clever Bash History Suggest Box.
When you are working at the command line, you often need to repeat or slightly modify previously entered commands. Most experienced users know they can browse the Bash history quite simply by pressing the up and down arrows at the command line, then pressing the left and right arrows to move the text cursor.
Fewer users know that you can also search the Bash history by pressing Ctrl+R and then typing; the shell automatically retrieves the first matching result. Continuing to press Ctrl+R jumps from match to match until you find the command you are seeking.
Bash keeps track of all the commands typed at the command line in the ~/.bash_history
file, and you can view the file with the history
command (Listing 1).
Listing 1
history Command Output
Although the Bash history and its search function deliver very useful results, the standard tools leave room for improvement. If you type Ctrl+R too often, and overshoot the desired target, you have no way to go back one step in the history. You have to cancel the search (Ctrl+ D) and start all over again.
If you spend a lot of time at the console, you will not want to miss the Bash History Suggest Box utility [1], which improves the view of your Bash history and supports more convenient searching (Figure 1).
Installation
The Bash History Suggest Box tool has not yet found its way into the repositories of the major distributions. Even Ubuntu 14.04 "Trusty Tahr" does not yet have the software in its repositories, but you can install it retroactively thanks to a PPA provided by the tool's developer:
$ sudo add-apt-repository ppa:ultradvorka/ppa $ sudo apt-get update $ sudo apt-get install hh
Arch Linux users find the Bash History Suggest Box in AUR [2]; for Debian "Wheezy" and Linux Mint 13, the program author recommends an additional package source on his home page. However, you can also build Suggest Box yourself from the sources (Listing 2).
Listing 2
Building Suggest Box from Source
After completing the install, you can run Bash History Suggest Box by typing hh
. As usual, you scroll through the history with the up and down arrow keys and accept the currently selected command by pressing the left or right arrows or the Tab key. Alternatively, you can map the call to hh
directly to the Ctrl+R keyboard shortcut so that Suggest Box replaces the standard Bash search.
The following commands add the necessary configuration, together with additional settings for color and a more extensive bash history to your ~/.bashrc
, and then tells the shell to immediately re-parse its configuration:
$ hh --show-configuration >> ~/.bashrc $ source ~/.bashrc
This prevents having to log out and then back in to enable Suggest Box.
New Search Functions
Bash History Suggest Box does not just improve the history experience, it also enhances searching. As before, you launch a search by pressing Ctrl+R and then enter a search term. Suggest Box then automatically filters all the commands containing the search key from the history (Figure 2). If you want to remove one of the results from history, simply delete it by pressing the Delete key. To add frequently used commands to your favorites, press Ctrl+F.
The more often you execute a command, the higher up it appears in the search results. Pressing Ctrl+Shift+7 lets you sort the results by "ranking" or "history," or restrict the selection to your previously saved favorites. Pressing Ctrl+T tells Suggest Box to make case-sensitive searches.
The ability to exclude commands from the shell history is also useful. For example, if the history includes logins and passwords that you not want to appear in the Bash log, Suggest Box automatically adds the HISTCONTROL=ignorespace
command to your ~/.bashrc
:
export HISTCONTROL=ignorespace
If you add a space in front of a command, Bash does not add it to the history. You can also completely ignore trivial commands with HISTIGNORE
export HISTIGNORE="pwd:ls:ls -l:cd"
for example, ls
, pwd
, or cd
.
Conclusions
Compared with the standard Bash search, Bash History Suggest Box offers numerous advantages. First and foremost, you can scroll through the search results and quickly retrieve and rewrite a command from the history. Additionally, having the ability to sort the search results by input frequency is particularly practical: It makes sure that you will always find the most frequently used commands at the top of the list.
Infos
- Bash History Suggest Box: https://github.com/dvorka/hstr
- hh in AUR: https://aur.archlinux.org/packages/hh/
Buy this article as PDF
(incl. VAT)
Buy Linux Magazine
Subscribe to our Linux Newsletters
Find Linux and Open Source Jobs
Subscribe to our ADMIN Newsletters
Support Our Work
Linux Magazine content is made possible with support from readers like you. Please consider contributing when you’ve found an article to be beneficial.
![Learn More](https://www.linux-magazine.com/var/linux_magazin/storage/images/media/linux-magazine-eng-us/images/misc/learn-more/834592-1-eng-US/Learn-More_medium.png)
News
-
Linux Servers Targeted by Akira Ransomware
A group of bad actors who have already extorted $42 million have their sights set on the Linux platform.
-
TUXEDO Computers Unveils Linux Laptop Featuring AMD Ryzen CPU
This latest release is the first laptop to include the new CPU from Ryzen and Linux preinstalled.
-
XZ Gets the All-Clear
The back door xz vulnerability has been officially reverted for Fedora 40 and versions 38 and 39 were never affected.
-
Canonical Collaborates with Qualcomm on New Venture
This new joint effort is geared toward bringing Ubuntu and Ubuntu Core to Qualcomm-powered devices.
-
Kodi 21.0 Open-Source Entertainment Hub Released
After a year of development, the award-winning Kodi cross-platform, media center software is now available with many new additions and improvements.
-
Linux Usage Increases in Two Key Areas
If market share is your thing, you'll be happy to know that Linux is on the rise in two areas that, if they keep climbing, could have serious meaning for Linux's future.
-
Vulnerability Discovered in xz Libraries
An urgent alert for Fedora 40 has been posted and users should pay attention.
-
Canonical Bumps LTS Support to 12 years
If you're worried that your Ubuntu LTS release won't be supported long enough to last, Canonical has a surprise for you in the form of 12 years of security coverage.
-
Fedora 40 Beta Released Soon
With the official release of Fedora 40 coming in April, it's almost time to download the beta and see what's new.
-
New Pentesting Distribution to Compete with Kali Linux
SnoopGod is now available for your testing needs