4

When doing some installations I change the 'HISTFILE' variable to a different file to record the commands used. Now I want to use the history command to display them just like with the history command which defaults to using the .bash_history file.

What option should be passed to the history command? When I try history ~/.history.d/alternate_historyI get the error message

-bash: history: /home/vfclists/.history.d/alternate_history: numeric argument required.

The man help lists some options which appear to make some changes to other history files I don't want.

  • 1
    I don't believe history ever operates on a file, rather than the in-memory history list. You'd have to append to/replace your current entries, or start a new shell. Is that workable for you? – Michael Homer Oct 13 '16 at 6:35
  • @MichaelHomer I don't want to overwrite or replace the current history. I just want to display it like a the normal history contents. Normally I would use the cat command, but because this history includes dates I get epoch times followed by the commands which I don't want. I probably have to write short utility for that. – vfclists Oct 13 '16 at 8:03
4

The history command never operates on a file, only on its in-memory history list. You can only read (r), write (-w), and append (-a) that list to or from a file, and then access or manipulate the in-memory list. Reading from a file will replace or extend the history in your current shell session.

You can, however, spawn another shell and manipulate its history to run any command you want without affecting the history of your current shell:

bash -c 'history -cr file ; history'

or

( history -cr file ; history )

You can add any history options you want to the second history command in either case. If you'll be doing this a lot, you may want to define a function accepting the file as an argument and running the subshell version:

histfile() {
    ( history -cr "$1" ; history )
}

If you're interested in displaying saved timestamps, you'll also need to set HISTTIMEFORMAT. If you're using a subshell, and you get timestamps in your host shell, that should be there automatically, but for the bash -c version or a script you'll need to set it:

bash -c 'history -cr file ; HISTTIMEFORMAT="%Y%m%d%H%I%S " history'

You can also export the variable from the parent shell.

  • The history file also contains timestamps. Is there a way to get the timestamps to be displayed as well? – vfclists Oct 13 '16 at 14:03
  • 2
    @vfclists You need to set HISTTIMEFORMAT. If you've got that set up in your parent shell already you should just be able to export it, but otherwise you can set it to whatever you want (see new last paragraph above). – Michael Homer Oct 13 '16 at 19:31
3

The current history for your current shell is stored in memory, it is only written to the HISTFILE once the shell is exited.

You can cause this to happen before you exit the shell with history -a. There are also arguments to read the contents of a file into the current history etc. Essentially it's a RTFM :-)

So you could write out the current history with history -a temp_history_file, then read in the new ones with history -r new_history_file. Your command history would then have the commands from new_history_file as it's most recent entries.

  • I don't want to replace any files or the current history. I will probably have to write my own utility if one doesn't exist already. See my response to @MichaelHorner in the comment above. – vfclists Oct 13 '16 at 8:06
  • Understood. You'll just need to manipulate the current in memory history to your needs. Michael Homer's suggestion of spawning a new shell in which to do it is a good one. – Unbeliever Oct 13 '16 at 8:16

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.