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
Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.
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.
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
I solved this by just attaching the file with
history -a current-history cat current-history >> .bash_history
history -a with a filename. This can be a device-file, like
Then you can pipe that however you like. For example, to view your current session history in
less you can
history -a /dev/stdout | less
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 <( history | cut -c 8- ) ~/.bash_history | sed -n 's/^< //pg'