Monday, December 25, 2017

more about bash history

Some time ago, I posted a few tips for calling up previously run commands in "searching bash history". Using those and other bash history tricks saves me lots and lots of time at the command line.

The following command outputs everything from bash history:

$ history

Since that command can result in a very long list, sometimes it's helpful to pipe it into less:

$ history | less

From there, press Enter to scroll down the list one line at a time, or press Space to scroll down a page at a time. Press Q to exit less. (See man less for more about the less command).

Often, it's easy enough to simply use the up and down arrow keys from the command line to scroll through the history of previously run commands.

The history n command will list the previous n commands you've run. For example, the following lists the previous 10 commands:

$ history 10

As I mentioned in my earlier post, the Ctrl+R keystroke starts a "reverse-i-search". I normally have to type no more than three letters after that to find the command I'm looking for. Pressing Ctrl+R again finds an earlier match; Enter executes the command. The left or right arrow key will place the command onto the command line for editing; Ctrl+G exits the reverse search.

For a lot more info, check out the documentation in man bash. See "Searching" in the READLINE section, and check out the HISTORY section, and the HISTORY EXPANSION section.

man bash is such a long document, though. I've found it helpful to bookmark a couple of nice web pages for some good, quick info:

http://www.tldp.org/LDP/GNU-Linux-Tools-Summary/html/x1712.htm

https://www.digitalocean.com/community/tutorials/how-to-use-bash-history-commands-and-expansions-on-a-linux-vps

No comments: