Maybe I'm overlooking something but is there a way to get your current bash history for the current session you are using like

if i run

ssh host
$ pwd
$ ls
$ cd /tmp

I just want to see those 3 commands and nothing else

  • have you checked history command?? – Hackaholic Dec 3 '14 at 21:52
  • ya i know about history but that doesn't give just my session – Mike Dec 3 '14 at 23:21
  • The amount saved between sessions is controlled by the SAVEHIST shell variable if I recall. Thus if the amount of history you saved last time was ZERO, then you'd get history for the current shell session (but of course not at the sub levels) – mdpc Dec 3 '14 at 23:27
  • @mdpc, what docs are you reading? In the Bash man page I find no reference to SAVEHIST but only to HISTSIZE and HISTFILESIZE. – Wildcard Feb 1 '17 at 7:54

A slightly roundabout way:

history -a ~/current_history

This will save the current session's unsaved bash history to ~/current_history, which you can then view.

| improve this answer | |
  • this could work but doesn't handle multiple sessions.. I supposed i can create a random temp file to store it in – Mike Dec 3 '14 at 23:21
  • 1
    @Mike use a name connected with the shell PID, perhaps ~/$$_history. – muru Dec 3 '14 at 23:23
  • 4
    It's worth noting that this won't clear out the history or affect the normal history saving in any way! I wasn't sure until I tested it. :) – NHDaly Jan 29 '15 at 2:10
  • I find it more convenient to just do history -a it throws it along with the previous history into ~/.bash_history ... which could be a problem if your history is longer than the cutoff, which is a 1000 on my centos7 and fedora 24 systems. – Ray Foss Jun 5 '17 at 20:53

Use comp to compare the entire history (incl. current Bash session) with the already persisted history in .bash_history and only print those lines that are unique to the current session -- which should show only those commands that were executed since starting the current Bash shell

comm -23 <( history | cut -c 8- ) ~/.bash_history

Edit: as @Wildcard pointed out this command does not work for all distributions of comm. I tested this on Mac OS.

A variation of the same idea using diff:

diff <( history | cut -c 8- ) ~/.bash_history | sed -n 's/^< //pg'
| improve this answer | |
  • A good thought, but very unreliable in my tests. comm assumes sorted input. But, welcome to Stack Exchange! :) – Wildcard Feb 1 '17 at 4:12
  • @Wildcard, both sides of the input to comm are sorted inherently in my example above. Did you run it? And it did not produce your current Bash session history? – Christian Kadner Feb 1 '17 at 4:21
  • Yes, I did, and no, it didn't. And no, the contents of ~/.bash_history is not sorted; and nor is the output of history sorted at all once leading line numbers are removed. (Besides which, comm expects lexicographically sorted input, not numerically sorted input.) – Wildcard Feb 1 '17 at 4:29
  • Right, I meant both arguments to the comm command in my example are in the same order, not that they are sorted lexicographical. The FreeBSD version of comm does not require lexicographically sorted input – Christian Kadner Feb 1 '17 at 4:41
  • I'm using Mac OS X. It has nothing to do with that. Looking closer, it appears to be the early commands from the in-memory history, which have since been overwritten in the history file by other shells exiting. – Wildcard Feb 1 '17 at 4:53

I had the problem that I wanted to write the current history to a file but still wanted the entries to be recorded in the main bash history

I solved this by just attaching the file with cat:

history -a current-history
cat current-history >> .bash_history
| improve this answer | |

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.