Run statistics on typed shell commands

Programming Snapshot – Shell Stats

© Lead Image © Scott Rothstein, 123RF.com

© Lead Image © Scott Rothstein, 123RF.com

Article from Issue 243/2021
Author(s):

In the history log, the Bash shell records all commands typed by the user. Mike Schilli extracts this data with Go for a statistical analysis of his typing behavior.

How did the long command to connect to the database server work again? Shell power users know this memory problem and have taught themselves tricks over the years to repeat or resend modified versions of previously sent command lines. For example, the sequence !! repeats the last command (useful to prepend a sudo you might have forgotten); with Ctrl+R you can find and reuse commands that were sent further back in time by using search patterns. Bash remembers the entire typing history and shows it with the history command, which lists the tail end of what it has in memory (Figure 1).

If you have ever taken a look at the .bash_history file in your home directory, you will know its little secret. This is where Bash simply appends every command sent, regardless of if it was successful or aborted with an error. If you ask for the last commands you entered, Bash simply looks there (Figure 2).

Timestamp It!

With a small change, Bash not only logs typed commands, but even adds the date and time when the commands were entered. To do this, you need to set the following environment variable (preferably in the .bash_profile init file):

[...]

Use Express-Checkout link below to read the full article (PDF).

Buy this article as PDF

Express-Checkout as PDF
Price $2.95
(incl. VAT)

Buy Linux Magazine

SINGLE ISSUES
 
SUBSCRIPTIONS
 
TABLET & SMARTPHONE APPS
Get it on Google Play

US / Canada

Get it on Google Play

UK / Australia

Related content

  • Effectively using Bash history

    You can do more with the Bash history command than just using the arrow keys. We show you how to use this command-line tool more efficiently.

  • Bash History on Steroids

    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.

  • McFly

    When it comes to working at the command line, using Bash history effectively can save you time. McFly extends the Bash history's features and helps you find past commands more quickly.

  • History Lesson

    For admins like Charly, who try to avoid typing at all costs, the shell offers an excellent opportunity to avoid wear on your fingertips in the form of built-in history.

  • Command Line

    A few basic tricks can liven up the command line and add a dash of color to your console.

comments powered by Disqus

Direct Download

Read full article as PDF:

Price $2.95

News