Is it possible to retrieve the actual commands typed in a bash history? If the command involved (for example) a history substitution or modifier, what is seen in the history is the resulting command rather than the typed command.

A couple of examples. For both of these, suppose that command 123 in the history is ls Red*

  1. If I type in !123 to use command line 123 from the history, and now look at the latest command in my history I will see ls Red* rather than !123.

  2. If use the built-in shell command fc -s Red=Blue 123 and look at the history, I will see ls Blue* rather than fc -s Red=Blue 123

Similarly, if I use the up-arrow (producing the escape sequence ^[[A) I get the expanded command rather than the actual typed text.

In all of these examples the resulting entry in the history is what I expect but in some cases it would be useful to retrieve the originally typed text. For example, whilst the above examples are quite simple I might be using a complex substitution that I want to edit.

I realise that this would be further complicated after the shell is initialised as a HISTFILE does not contain this information. I am not concerned with retrieving the original text in this case (although it is interesting).

  • This is not an actual answer to your question, but — if you are on a terminal that allows copy and paste (and you are still logged in; i.e., not trying to recover the keystrokes you typed last week), it's often beneficial to copy and paste old commands.  For example, if I want to repeatedly alternate between two commands, I might type ls Red*, ls Blue*, and !-2 — and then copy the !-2 and paste it repeatedly. You might also want to look at a program called "screen". – Scott Jul 23 '16 at 15:32

The actual answer to the question you asked is "No, bash does not have a feature like that". It was something I thought about a decade ago when I was working in the same building as the core GNU team, and so I went and talked to them and, apparently, adding this would be a PITA (including figuring out the syntax and storing all that extra history). A lot of people would occasionally benefit, but there were lots of bigger issues to address, so it was unlikely to get fixed.

On the other hand, I have often found that using X11's cut-and-paste features can address my actual needs in almost every case. It's made even better with the discovery of the following bit of XResource magic:

*cutToBeginningOfLine:      False

If you know where your XResources are, then you can add this line to that file. If you can't determine that, you could just add echo '*cutToBeginningOfLine: False'|xrdb -merge to one of your startup files (e.g. .profile, but depends on your shell).

What this does is make triple click on the left mouse button copy the word you're pointing at to the end of the line (including the Enter), and then you can paste it easily. By default (i.e. without this) triple click copies the whole line including the prompt.

  • Interesting history, thank you. Do you happen to know if the feature exists in any of the other common shells? – borrible Jul 24 '16 at 11:09
  • Nope, I'm strictly a bash person. Having known the early bash developers personally might have something to do with that. – MAP Jul 24 '16 at 11:11

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.