Take the 2-minute tour ×
Ask Ubuntu is a question and answer site for Ubuntu users and developers. It's 100% free, no registration required.

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

share|improve this question

3 Answers 3

up vote 18 down vote accepted

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:

share|improve this answer
    
isn't it stored separately for different TTL?? –  Vineet Menon Nov 18 '11 at 7:02
    
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 7:07
    
@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 7:36

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.$$
# 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).

share|improve this answer
1  
Why do you assign HISTFILESIZE twice? –  Daniel Oct 11 '12 at 7:26
1  
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 4:20
    
is there a way to merge the history files in order to make Ctrl+r work again? –  naxa Mar 14 '13 at 16:45
    
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 0:46

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/comment-page-1/index.html#comment-640

share|improve this answer
    
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 at 14:16

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.