Enhancing efficiency with history
Command Line – Bash History

© Photo by Federico Di Dio photography on Unsplash
The versatile Bash history command can save you time and effort at the command line.
If you work in a terminal, you've likely used Bash's history
command to save yourself the trouble of retyping a command [1] (Figure 1). However, if you're like most people, your use of history
may have been confined to scrolling through the list of previously used commands. If all you are interested in are the most recently used commands, the arrow keys may be all that you need. However, the history
command is capable of doing much more and in an economical way – especially if you have a good memory. You can start by adjusting history
's environmental variables and then learn how to modify history entries for easier searching and for repurposing them using three types of editing options: event designators, word designators, and modifiers. The flexibility of all these options can be combined so that, with a little memorization, you can make the Bash history work for you to save time with minimal effort.
Environmental Variables
The history
commmand has several environmental variables. All are added or modified in .profile
or .bashrc
in your home directory, depending on the distribution. The size of the history file is limited by HISTSIZE
, which sets the number of entries in the history, and/or by HISTFILESIZE
, the maximum memory to allot for the history. Both have similar structures:
HISTSIZE=NUMBER HISTFILESIZE=NUMBER
When the maximum for either variable is reached, earlier entries are deleted and replaced by new ones. Many users' first impulse is to use a high number, such as 10,000 entries. Because the history is a text file, there should usually be no problem with how much space the history file occupies. However, too large a number can make locating entries much harder. Unless you have a clear need for such a large number, a smaller one can be more efficient. In any event, you can use Ctrl+R to cycle through entries. If you have a rough idea of where an entry might be, you can use history NUMBER
to list the entries displayed, starting with the most recent, or a specific number to go directly to the entry. You can even clear your history with the command history -c
, followed by history -w
if a long history gets too confusing. Learning designators and modifiers will also make a longer history easier to use.
Another environmental variable for history
is HISTTIMEFORMAT
. As the name suggests, this variable adds a timestamp, which can help you locate entries more easily. The format is HISTTIMEFORMAT=DATE&TIME
(Figure 2).
The date and time are structured using the common values shown in Table 1. Their use and order is a matter of choice, but you may soon consider HISTTIMEFORMAT
essential.
Table 1
Values for Setting HISTTIMEFORMAT
%d |
Day |
%m |
Month |
%Y |
Year |
%H |
Hours |
%M |
Minutes |
%S |
Seconds |
Values can also be assigned to HISTCONTROL
. A value of ignorespace
excludes any command that has a space before it, while ignoredup
ignores one of the same commands when run one after the other. If you want to use both variables, use ignoreboth
.
Event Designators
An event designator calls on a specific previous command and always starts with an exclamation mark (!
). In its simplest form, an event designator follows !
with a command's number in the history. However, as you might expect, from a user account, you cannot run a command such as apt
that can only be run as root (Figure 3). You can also call a command by how many previous commands ago it was used; for example, !-4
shows the command used four entries ago. Alternatively, you can enter a string so that !pipewire
shows the last command that contains "pipewire." You can even find a string and replace it with another. For example, if you typed
cd /home/jlw/music

you could replace the cd
command with ls
with ^cd^ls
and save yourself the trouble of retyping the path.
Word Designators
Word designators select the words from the most recent matching entry in the history. To use word designators, enter the new command and its options, followed by EVENT:!WORD-DESIGNATOR^
. For example,
less !cat:^
will replace the most recent history entry found starting with cat
with less
and then run the rest of the command in the entry using less
. The word designator can be a string or specify the word at an exact position, counting from the start of the event. For example, is the first word, 3
is the fourth word, and $
is the last. In addition, you can specify a range of words, such as 4-8
, or every word except the first using an asterisk (*
) if you recall enough to be specific.
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.
News
-
Rocky Linux 9.3 is Available for Download
The latest version of the RHEL alternative is now available and brings back cloud and container images for ppc64le along with plenty of new features and fixes.
-
Ubuntu Budgie Shifts How to Tackle Wayland
Ubuntu Budgie has yet to make the switch to Wayland but with a change in approaches, they're finally on track to making it happen.
-
TUXEDO's New Ultraportable Linux Workstation Released
The TUXEDO Pulse 14 blends portability with power, thanks to the AMD Ryzen 7 7840HS CPU.
-
AlmaLinux Will No Longer Be "Just Another RHEL Clone"
With the release of AlmaLinux 9.3, the distribution will be built entirely from upstream sources.
-
elementary OS 8 Has a Big Surprise in Store
When elementary OS 8 finally arrives, it will not only be based on Ubuntu 24.04 but it will also default to Wayland for better performance and security.
-
OpenELA Releases Enterprise Linux Source Code
With Red Hat restricting the source for RHEL, it was only a matter of time before those who depended on that source struck out on their own.
-
StripedFly Malware Hiding in Plain Sight as a Cryptocurrency Miner
A rather deceptive piece of malware has infected 1 million Windows and Linux hosts since 2017.
-
Experimental Wayland Support Planned for Linux Mint 21.3
As with most Linux distributions, the migration to Wayland is in full force. While some distributions have already made the move, Linux Mint has been a bit slower to do so.
-
Window Maker Live 0.96.0-0 Released
If you're a fan of the Window Maker window manager, there's a new official release of the Linux distribution that champions the old-school user interface.
-
KDE Plasma 6 Sets Release Date
If you've been anxiously awaiting the release of the next big KDE Plasma milestone, you can rest assured it's sooner than you think.