164

I use mainly Terminator, and it's usually opened with 3 split terminal windows. I also use Gnome terminal for various reasons.
I'm wondering how is bash history handled in this case as I sometimes miss previously issued commands when I run history

For example, my prompt shows current bash history line (\!) and if I launch Terminator with 3 split terminal windows I get same history line (let's say 100) on all terminals. Which history will be saved?

Also launching Gnome Terminal after using Terminator I get line 100 at startup regardless all commands issued before in Terminator

muru
  • 207,228
zetah
  • 9,871

7 Answers7

159

The bash session that is saved is the one for the terminal that is closed the latest. If you want to save the commands for every session, you could use the trick explained here.

export PROMPT_COMMAND='history -a'

To quote the manpage: “If set, the value is executed as a command prior to issuing each primary prompt.”

So every time my command has finished, it appends the unwritten history item to ~/.bash_history before displaying the prompt (only $PS1) again.

So after putting that line in /etc/bash.bashrc I don’t have to find myself reinventing wheels or lose valuable seconds re-typing stuff just because I was lazy with my terminals.

Anyway, you'll need to take into account that commands from different sessions will be mixed in your history file so it won't be so straightforward to read it later.

See also:

jcollado
  • 9,868
45

After multiple readings of man bash, I use separate history files for each shell. I did a mkdir -m 0700 ~/.history then added

[[ -d ~/.history ]] || mkdir --mode=0700 ~/.history
[[ -d ~/.history ]] && chmod 0700 ~/.history
HISTFILE=~/.history/history.$(date +%y%b%d-%H%M%S).$$
# close any old history file by zeroing HISTFILESIZE  
HISTFILESIZE=0  
# then set HISTFILESIZE to a large value
HISTFILESIZE=4096  
HISTSIZE=4096  

to my ~/.bashrc. Every now and then, I remember to du -sk .history and clean it out. It's nice to have every command I've typed preserved for me.

I just used the above to see what I'd been doing, of late:
cut -f1 "-d " .history/* | sort | uniq -c |sort -n -r |less
or
cut -f1-2 "-d " .history/* | sort | uniq -c |sort -n -r |less
(to include the 1st argument e.g. sudo mount in the sort chain).

waltinator
  • 37,856
19

History from all shell sessions is less useful than I hoped!

I enthusiastically set PROMPT_COMMAND='history -a; history -r' in the hopes that my history usage would be much better!

Wow, was I disappointed.

Essentially, doing this makes up/dn arrow useless. Because now each history session is littered with commands from other sessions. And for me this loses the best feature of history.

What I really wanted

Occasionally, I wanted to essentially transfer my history from one terminal session to another session so I could easily recall commands from that session. And I can do this very selectively.

What I did

I set up a series of simple history manipulation aliases & I ignore these commands in the history:

alias ha="history -a"
alias hb="history -a; history -r"
alias hr="history -r"
alias hl="history | tail -20"
HISTIGNORE="ha:hb:hr:hl"

I think of hb as history blend...the others are self-explanatory.

Now when I want to copy my history from session A to session B, I can do this:

  • A: ha - append session A history to history file
  • B: hb - append session B history to history file and then read the file

This essentially preserves the history order for session B and adds session A history deeper in the list.

Super useful for me. Maybe helps you too!

BruceJo
  • 299
11

See also "keeping persistent history in bash" for another alternative. It rigs your prompt to send all commands ever typed into any terminal into a "persistent history" file (alongside what's usually done for the regular .history).

10

To show history from all terminals:

Add export PROMPT_COMMAND='history -a; history -r' to your .bashrc file.

Source: http://northernmost.org/blog/flush-bash_history-after-each-command


Elijah Lynn
  • 3,928
5

If you want a more complete solution that persists your bash history locally, stores additional context (cwd, runtime, exit code, etc) and syncs it to your other computers, see hishtory. I got frustrated with shell histories misbehaving, and this is my solution to the problem.

0

@brucejo Building on the answer from @waltinator (https://askubuntu.com/a/80882/1679924 ), for Ubuntu i have a small script that i put in my bin folder. Typically i have a few terminals for specific projects, (that i name proj-server, proj-mobile, proj-frontend .. etc) so i can open a gnome-terminal with a {name} and save the history to a file ~/.history/history.{name}

#!/usr/bin/bash

if [ -z "$1" ]; then export TERMINAL_NAME="default" else export TERMINAL_NAME="$1" fi

echo "Starting gnome-terminal $TERMINAL_NAME"

HISTDIR="$HOME/.history"

[[ -d "$HISTDIR" ]] || mkdir --mode=700 "$HISTDIR" [[ -d "$HISTDIR" ]] && chmod 700 "$HISTDIR" export HISTFILE="$HISTDIR/history.$TERMINAL_NAME"

export PROMPT_COMMAND='history -a'

echo "Appending history to $HISTFILE" gnome-terminal -p