Bash, the shell that CentOS users interact with on the command line, is almost 30 years old. As a text-only interface, it has, over the years, developed many small conveniences for making the process of entering, recalling, and editing commands as efficient as possible. Among the most useful is Bash’s command history.
If you’ve used the Linux command line for more than a few minutes, you probably already know that you can navigate that history using the up and down cursor keys. By hitting the up arrow, you navigate through a list of commands in reverse chronological order. But there’s a lot more to Bash’s history that many server hosting clients don’t know about.
One of the most useful features is reverse incremental search. Hit CTRL-R and type a few letters from a previously entered command. To search through all the matches, repeatedly hit CTRL-R until you find what you’re looking for, and then hit ENTER to run the command. If necessary, you can also edit the command before hitting running it.
Bash also provides useful operators for interacting with the command history.
Like all of us, I’m sure you’ve run a command and realized only afterwards that it requires root privileges. You can hit the up arrow, navigate to the beginning of the command, and enter sudo, but this is quicker:
The !! operator is replaced with the previous command.
Also useful is the ! operator, which will search for a string and run the first matching command. So, if you have recently run a grep command and want to run it again:
The operators we’ve looked at deal with whole commands, but what if you only want the arguments? I often find myself needing to run different commands on a selection of files or the same command with different options.
Of course, Bash has a solution for this too. Let’s say I run a command like
The !* operator expands to all of the arguments given to the last command. Its relations !^ and !$ expand to the first and last arguments respectively.
Typos are among the most annoying aspects of working on the command line. It’s frustrating to type a long and complex set of commands, only to have them fail because of a typo right at the beginning. For some reason, I frequently type sl instead of ls.
sl -l directory1 director2 directory3
That won’t work, but I don’t want to type the command again or laboriously arrow back through it to the mistake. Instead, I can do this:
This runs the previous command, replacing the string after the first ^ with the string after the second ^. For such a short command, it’s overkill, but for multiline commands it saves me a lot of frustration.
All of the history operators pull information from the .bash_history file, which is located in your home folder. This is an ordinary text file, so you can edit it with vi and look at it with less
Learning to work with your Bash history will make you a more efficient and effective command line user. If you want to find out more about tools for working with your Linux server’s command history, man fc is a good place to start.