158

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
  • 197,895
  • 55
  • 485
  • 740
zetah
  • 9,673

7 Answers7

151

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,598
  • isn't it stored separately for different TTL?? – Vineet Menon Nov 18 '11 at 07:02
  • 2
    Excellent. Thanks for explanation and solution. I tried with export PROMPT_COMMAND='history -a; history -r' and I got some strange history line numbers in terminal - after issuing some command history line number jumps by 2000 instead by 1, which is strange but it behaves as said - all terminals history is saved. – zetah Nov 18 '11 at 07:07
  • 3
    @VineetMenon For more details look here. Interesting parts: When the shell starts up, the history is initialized from the file named by the HISTFILE variable (default `~/.bash_history'). [...] When an interactive shell exits, the last $HISTSIZE lines are copied from the history list to the file named by $HISTFILE. – jcollado Nov 18 '11 at 07:36
  • This website seems down: https://northernmost.org/blog/flush-bash_history-after-each-command/ – Shubham Oct 17 '17 at 11:37
  • 3
  • 1
    "won't be so straightforward to read it later." How do you work? I usually work on one terminal at a time following a thread of thought ;-) The history shouldn't be much worse than your train of thought... – lmat - Reinstate Monica Jul 18 '19 at 18:40
  • I tested this and found that after setting PROMPT_COMMAND the commands that appear when running "history" are not updated immediately in memory for each open terminal, but rather when you open a new terminal. This matches the explanation in jcollado's comment. – Joshua M Oct 29 '22 at 15:19
44

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
  • 36,399
  • 3
    Why do you assign HISTFILESIZE twice? – Daniel Oct 11 '12 at 07:26
  • 2
    By setting HISTFILESIZE to 0, I clear the history buffer and reset the history saving mechanism. Then, I set the size I really want, and start saving history in HISTFILE. See the HISTORY section of man bash. – waltinator Oct 18 '12 at 04:20
  • 2
    is there a way to merge the history files in order to make Ctrl+r work again? – n611x007 Mar 14 '13 at 16:45
  • 3
    This should be a new question, but I don't think it's a Good Idea. I use egrep 'whatever' .history/* (or cat .history/* | egrep 'whatever') instead, and use Ctrl-r to search an individual session's history. Read man bash-builtins about the history command. My sort .history/* | uniq -c | sort -n | wc -l shows 16033 unique commands, cut '-d ' -f1 .history/* | sort | uniq -c | sort -n shows 2004 unique commands, mostly typos. Loading all that into bash's "history list" would not help. – waltinator Mar 18 '13 at 00:46
  • @waltinator - i know this is an old comment, but are you implying that setting an environment variable somehow triggers an immediate action? what process is going to pick up the fact you assigned a value of 0 to HISTFILESIZE in a local shell and then immediately change to 4096? is this something undocumented about bash's handling of certain variable names? – unsynchronized Apr 24 '19 at 02:40
  • @Daniel To invoke the Deep Magic. – lmat - Reinstate Monica Jul 18 '19 at 18:41
  • @unsynchronized: See man bash, the part that starts withThe following variables are used by the shell. – waltinator Aug 22 '19 at 03:54
  • (+1) Is it actually necessary to first run mkdir -m 0700 ~/.history, then also include it in the second condition in your first line of the code block: [[ -d ~/.history ]] || mkdir --mode=0700 ~/.history ? Do the first two lines not ensure (with each new terminal when .bashrc is executed) that the folder exists with the expected permissions? – n1k31t4 Jan 15 '20 at 00:05
  • @n1k31t4 No, not actually necessary, but included for documentation. It's easier to simply set the permission to "what's Right", than to check with stat -c "%a" ~/.history/ and then fix it if it's wrong. Yes, the two lines ensure that, every time through ~/.bashrc, my ~/.history directory exists with the Right permissions. – waltinator Jan 15 '20 at 20:07
16

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
  • 269
  • 1
    Use ctrl+R to search your history from terminal, that will help with mixed shells... – Enissay Dec 11 '21 at 02:00
  • This was what I really wanted. Most shells I don't want to keep history, I'm just 'doing stuff', but whenever there's a command I want to use in ctrl+r a lot, I can just type ha and then new shells will have that terminals history available to ctrl+r. To add to live session, hb ... this is my preferred solution. – Ajax Jun 04 '23 at 19:58
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).

  • 1
    I inserted $$ to make the line echo $$ $date_part "$command_part" >> ~/.persistent_history This prefixes the entries with the PIDs of their bash process, so that we can disentangle multiple histories. – Evgeni Sergeev Mar 29 '18 at 03:11
  • 3
    Can the alternative history be made to work with CTRL-R history search? – max Dec 03 '18 at 21:53
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,828
  • 3
    I will say that after a couple months of using this I eventually commented this out just recently. It is bittersweet as I don't always want to sift through the last 100 commands just to run the one I had originally run in the Tmux pane I sometimes may have a dedicated log command in that I need to restart. – Elijah Lynn Feb 13 '14 at 14:16
  • 2
    A nice solution that the fish shell offers is a history --merge. I have been using this for a year or so now and it solves the issue. When I want the most recent history from other sessions, I just run that command and it is available to me instantly. – Elijah Lynn May 07 '18 at 21:38
  • 1
    export is not needed. – lmat - Reinstate Monica Jul 18 '19 at 18:41
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.

user297944
  • 61
  • 1
  • 4
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