bash history for current session
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
Answers:
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.
Method 1
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.Method 2
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
Method 3
You use history -a
with a filename. This can be a device-file, like /dev/stdout
.
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
Method 4
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'
All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0