Home > command line > Bash history handling with multiple terminals

Bash history handling with multiple terminals

November 18Hits:0
Advertisement

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

Answers

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:

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

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

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

Related Articles

  • Bash history handling with multiple terminalsNovember 18

    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 Fo

  • Project Bashory - Bash HistoryOctober 11

    In effort to be more effective and productive with terminals I have started building a bash script. It is for handling the bash history of concurrently running terminals, archiving old commands, and dynamically loading bash history profiles. Most cur

  • How to preserve bash history in multiple terminal windows August 26

    I consistently have more than one terminal open. Anywhere from two to ten, doing various bits and bobs. Now let's say I restart and open up another set of terminals. Some remember certain things, some forget. I want a history that: Remembers everythi

  • Strange bash history behaviour when running multiple sessionsDecember 2

    How is command line history stored when I use multiple terminal windows? I know it is stored in .bash_history but I can't see the logic on what history is used if I open new window. It almost feels nondeterministic in a sense that I never know what c

  • Open multiple terminals in a for loop in bashSeptember 19

    Recently i changed to lxde on Ubuntu. And after switching it changed my x-terminal-emulator to lxterm. My script was doing the following: Go through a for loop and open a new terminal window and then run a script in that terminal. It would open multi

  • How do I keep my bash history across sessions?November 17

    I am working on a x86 target running fedora 9. Whenever I reboot it, my history returns to some state, and I do not have commands I did in the sessions before the reboot. What I have to change to have updated history I had before reboot? ------------

  • How to clear bash history completely?September 23

    I want to clear all before command from history of my server. I use history -c it seems all things are cleared but when I ssh to the server,all commands are still there. How can I clear them permanently? --------------Solutions------------- ~/.bash_h

  • BASH history truncated to 500 lines on each login

    BASH history truncated to 500 lines on each loginMarch 29

    For some reason, I cannot get my system to keep my BASH history after a reboot. Here are the relevant sections of my ~/.bashrc: shopt -s histappend PROMPT_COMMAND='history -a; updateWindowTitle' export HISTCONTROL=ignoredups export HISTSIZE=9999 expo

  • Use bash history file from old machine when setting up new machine?June 16

    I love my bash history. Sometimes I've issued really long commands and I really depend on my bash history to get them back quickly. I've increased my history size with: HISTSIZE=100000 HISTFILESIZE=200000 in my .bashrc I also have aliases: alias h='h

  • Why is my bash history not sorted by date?December 21

    My mac bash history looks like this (I'm copying just the last +100 lines): 2769 23/09/14 17:54:09nano ~/.bash_profile 2770 23/09/14 17:54:21nano ~/.bash_profile 2771 23/09/14 17:54:28source ~/.bash_profile 2772 23/09/14 17:54:32ngrok 3000 2773 24/09

  • How to protect against purge of bash history?March 13

    Following the answer: When closing multiple bash instances at the same time, there is a known race condition that may cause the history to be cleared. This occurs because there is no locking used when the bash history file is written. How can I preve

  • How to create multiple "terminals" for a linux workstation?November 3

    I wanted to figure out how to have multiple terminals (monitor, keyboard, and mouse) use a single centralized system. I guess at this point I can get computers for around $150, and use them as dumb terminals, but I would really like to centralize abo

  • Bash history: "ignoredups" and "erasedups" setting conflict with common history across sessionsAugust 7

    First of all, this is not a duplicate of any existing threads on SE. I have read these two threads (1st, 2nd) on better bash history, but none of the answers work - - I am on Fedora 15 by the way. I added the following to the .bashrc file in the user

  • How to remove duplicate entries from bash history? August 7

    First of all, this is not a duplicate of any existing threads on SE. I have read these two threads (1st, 2nd) on better bash history, but none of the answers work - - I am on Fedora 15 by the way. I added the following to the .bashrc file in the user

  • Remove a certain line from Bash history fileJanuary 31

    How can I remove a certain line from history's database? --------------Solutions------------- Just edit the file ~/.bash_history. You can achieve removal from the history file using the commandline in two steps: Typing history -d <line_number> delet

  • Bash script to manage multiple Java Development Kits installationsMay 18

    I'm writing a bash script to manage multiple JDKs, the script is very simple, you have to choose a directory when you store all the jdk's, and the script maintain a symlink to the current jdk'. TheJAVA_HOMEis always pointing to thesymlink`. In order

  • How to get shared bashed history among different tabsJune 23

    I used the answer in http://unix.stackexchange.com/a/1292/41729 to enable real-time shared history among separate bash terminals. As explained in the answer above, this is achieved by adding: # avoid duplicates.. export HISTCONTROL=ignoredups:erasedu

  • What could cause my bash history to be cleared unexpectedly?August 30

    Today I noticed that my bash history is cleared completely. I have neither executed history -c nor deleted the .bash_history file. Apart from deleting the .bash_history file and history -c, how can bash history be cleared? --------------Solutions----

  • Disable repeated items in bash historyApril 8

    Bash in Fedora 19 is really annoying me. Say I run a command: echo "Hello" Then I run another command: echo "World" Now I run the last command (echo "World") again N times by pressing up on my keyboard, followed by enter. (Ty

  • Bash history in script: `!#:*`August 11

    I have been reading through the study guides for the LPIC-1. echo "This is a sentence. " !#:* !#:1->text3 I'm having trouble understanding how the above line of code repeats the echo command multiple times. I know that it is using a feature o

Copyright (C) 2017 ceus-now.com, All Rights Reserved. webmaster#ceus-now.com 14 q. 2.284 s.